I wish to alert all nodes text only. I want to go nested node and get text only
$(document).ready(function() {
var th1=0;
var th2=0;
var th3=0;
var th4=0;
var th5=0;
var th6=0;
var tbtn_nbr=0;
var tspan_nbr=0;
var tp_nbr=0;
var ta_nbr=0;
var tul_nbr=0;
var tli_nbr=0;
var title=document.getElementsByTagName('title')[0].innerHTML;
var total_div=document.getElementsByTagName('div').length;
var total_p=document.getElementsByTagName('p').length;
var total_a=document.getElementsByTagName('a').length;
var total_span=document.getElementsByTagName('span').length;
getdata("div", 0);
function getdata(tag_name, ti)
{
//alert(tag_name); alert(ti);
if(tag_name!='div'){ti--;}
//alert(ti);
var total_tag_lenght=document.getElementsByTagName(tag_name).length;
for(var ti; ti<=Number(total_tag_lenght); ti++){
var tag_inner=document.getElementsByTagName(tag_name)[ti].innerHTML;
alert(tag_inner);
ti_nbr=ti+1;
var words = tag_inner.split(" ");
var str=words[0];
var nd = str.search("<div");
var np = str.search("<p");
var na = str.search("<a");
var ns = str.search("<span");
var nh1 = str.search("<h1");
var nh2 = str.search("<h2");
var nh3 = str.search("<h3");
var nh4 = str.search("<h4");
var nh5 = str.search("<h5");
var nh6 = str.search("<h6");
var nbtn = str.search("<button");
var nul = str.search("<ul");
var nli = str.search("<li");
//alert(np);
if(nd>0)
{
getdata("div", ti_nbr);
}
else if(np>0)
{ //alert("hello");
tp_nbr++;
//alert(tp_nbr);
getdata("p", tp_nbr);
}
else if(na>0)
{ //alert("hello");
ta_nbr++;
getdata("a", ta_nbr);
}
else if(ns>0)
{ tspan_nbr++;
getdata("span", tspan_nbr);
}
else if(nh1>0)
{ th1_nbr++;
getdata("h1", th1_nbr);
}
else if(nh2>0)
{ th2_nbr++;
getdata("h2", th2_nbr);
}
else if(nh3>0)
{ th3_nbr++;
getdata("h3", th3_nbr);
}
else if(nh4>0)
{ th4_nbr++;
getdata("h4", th4_nbr);
}
else if(nh5>0)
{ th5_nbr++;
getdata("h5", th5_nbr);
}
else if(nh6>0)
{ th6_nbr++;
getdata("h6", th6_nbr);
}
else if(nbtn>0)
{ tbtn_nbr++;
getdata("button", tbtn_nbr);
}
else if(nul>0)
{ tul_nbr++;
getdata("ul", tul_nbr);
}
else if(nli>0)
{ tli_nbr++;
getdata("li", tli_nbr);
}
else
{
//alert(tag_inner);
}
}
}
});
Since you have jquery, you can select elements using "all" selector $("*").
Then you can iterate on them using "each" function
Related
I'm trying to find any object that is near or around the logo.
By adding 1/3 of the logo height to its bounds and comparing all the pageitems with the bounds.
How to skip the parent object of the graphic link? for the below script
Here is the script:
function _clear_space_around_logo(){
var myDoc = app.activeDocument;
var myPages = myDoc.pages;
for(var a=0;a<myPages.length;a++){
var myGraphics = myPages[a].allGraphics;
for(var b=0;b<myGraphics.length;b++){
if(myGraphics[b].itemLink != null){
if(myGraphics[b].itemLink.name.search(/Logo/i)>0){
if(myGraphics[b].parent.rotationAngle == 0 || myGraphics[b].parent.rotationAngle == 180){
var geo0 = myGraphics[b].geometricBounds[0];
var geo1 = myGraphics[b].geometricBounds[1];
var geo2 = myGraphics[b].geometricBounds[2];
var geo3 = myGraphics[b].geometricBounds[3];
var requiredHeight = (geo2-geo0)/3;
var reuqiredGeo0 = geo0-requiredHeight;
var reuqiredGeo1 = geo1-requiredHeight;
var reuqiredGeo2 = geo2+requiredHeight;
var reuqiredGeo3 = geo3+requiredHeight;
}
else{
var geo0 = myGraphics[b].geometricBounds[0];
var geo1 = myGraphics[b].geometricBounds[1];
var geo2 = myGraphics[b].geometricBounds[2];
var geo3 = myGraphics[b].geometricBounds[3];
var requiredHeight = (geo3-geo1)/3;
var reuqiredGeo0 = geo0-requiredHeight;
var reuqiredGeo1 = geo1-requiredHeight;
var reuqiredGeo2 = geo2+requiredHeight;
var reuqiredGeo3 = geo3+requiredHeight;
}
var myPagesItems = myPages[a].allPageItems;
for(var c=0;c<myPagesItems.length;c++){
if(myPagesItems[c].itemLayer.visible == true && myPagesItems[c].visible == true){
var itemGeo0 = myPagesItems[c].geometricBounds[0];
var itemGeo1 = myPagesItems[c].geometricBounds[1];
var itemGeo2 = myPagesItems[c].geometricBounds[2];
var itemGeo3 = myPagesItems[c].geometricBounds[3];
if(itemGeo0>myPages[a].bounds[0] && itemGeo1>myPages[a].bounds[1]&&
itemGeo2<myPages[a].bounds[2] && itemGeo3<myPages[a].bounds[3]){
if(itemGeo2 > reuqiredGeo0 && itemGeo3 > reuqiredGeo1 &&
itemGeo0 < reuqiredGeo2 && itemGeo1 < reuqiredGeo3){
if(myPagesItems[c].constructor.name != "Group"){
try{
if(myPagesItems[c].itemLink.name.search(/Logo/i)==-1){
alert(myPages[a].name);
}
}
catch(e){
alert(myPages[a].name);
}
}
}
}
}
}
}
}
}
}
}
Thanks
#brianp311
Store the id of the parent container and skip it on your check.
var pid = myGraphics[b].parent.id;
Then...
if (myPageItems[c].id == pid) continue;
I have the following spreadsheet.
Spreadsheet
I need a script to count each agent email address (overall and based on client). I am struggling with arrays. I am new on scripting so still learning. So, on the Reports tab I need to know how many calls an agent made, then how many calls he made for Client 1, Client 2 and so on, how many appointments made - overall and for each client. I don't want to use =COUNTIFS() as this will be dynamic and in time there will be new agents.
I will make a script to get unique emails and paste it into a table and from there I need this script to count.
In the shared file I have my script, but I stuck there.
Thank you!
Build and Load Everything All At Once
I added a few more ranges to your reports sheet so that I could display all of the client data. The client names and agent emails have to be there because they are used as inputs in my data arrays and objects.
Here's the code:
function reportSummary()//This is final output
{
var br='<br />';
var agentA=uniqueItemArray(4);
var clientA=uniqueItemArray(5);
var clientAgents=emailAddressAssociatedWithEachClient();
var agentClients=clientsAssociatedWithEachEmail();
var callsByAgent=callsMadeByAgentTotal();
var callsByAgentToClient=callsMadeByAgentToClient();
var apptsByAgent=apptsBookedByAgentTotal();
var apptsByAgentForClient=apptsBookedByAgentForClient();
var ss=SpreadsheetApp.getActive();
var rptSh=ss.getSheetByName('Reports');
var rgA=["A2:C6","A8:C12","A14:C18"];
for(var i=0;i<rgA.length;i++)
{
var rg1=rptSh.getRange(rgA[i]);
var vA1=rg1.getValues();
vA1[2][1]=callsByAgentToClient[vA1[2][0]][vA1[0][0]];
vA1[3][1]=callsByAgentToClient[vA1[3][0]][vA1[0][0]];
vA1[4][1]=callsByAgentToClient[vA1[4][0]][vA1[0][0]];
vA1[2][2]=apptsByAgentForClient[vA1[2][0]][vA1[0][0]];
vA1[3][2]=apptsByAgentForClient[vA1[3][0]][vA1[0][0]];
vA1[4][2]=apptsByAgentForClient[vA1[4][0]][vA1[0][0]];
rg1.setValues(vA1);
}
var rg2=rptSh.getRange('E2:H6');
var vA2=rg2.getValues();
vA2[2][1]=callsByAgent[vA2[2][0]];
vA2[3][1]=callsByAgent[vA2[3][0]];
vA2[4][1]=callsByAgent[vA2[4][0]];
vA2[2][2]=apptsByAgent[vA2[2][0]];
vA2[3][2]=apptsByAgent[vA2[3][0]];
vA2[4][2]=apptsByAgent[vA2[4][0]];
rg2.setValues(vA2);
/*
var s='<strong>Clients Contacted by Agent:</strong>';
for(var i=0;i<agentA.length;i++)
{
s+=br + '<span style="color:#ff0000">' + agentA[i] + '</span>';
for(var j=0;j<agentClients[agentA[i]].length;j++)
{
s+=br + agentClients[agentA[i]][j];
}
}
s+=br + br + '<strong>Agents who Contacted Client:</strong>';
for(var i=0;i<clientA.length;i++)
{
s+=br + '<span style="color:#ff0000">' + clientA[i] + '</span>';
for(var j=0;j<clientAgents[clientA[i]].length;j++)
{
s+=br + clientAgents[clientA[i]][j];
}
}
s+=br + br + '<strong>Calls made by Agents:</strong>';
for(var i=0;i<agentA.length;i++)
{
s+=br + Utilities.formatString('<br />%s = %s',agentA[i],callsByAgent[agentA[i]]);
}
s+=br + br + '<strong>Calls made by Agents to Client:</strong>';
for(var i=0;i<agentA.length;i++)
{
for(var j=0;j<clientA.length;j++)
{
s+=br + Utilities.formatString('<br />%s<br />%s<br />%s',agentA[i],clientA[j],callsByAgentToClient[agentA[i]][clientA[j]])
}
}
var userInterface=HtmlService.createHtmlOutput(s);
SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Report Summary');
*/
}
function uniqueItemArray(column)
{
var ss=SpreadsheetApp.getActive();
var cntSh=ss.getSheetByName('Count');
var cntRg=cntSh.getDataRange();
var vA=cntRg.getValues();
var uiA=[];
for(var i=2;i<vA.length;i++)
{
if(uiA.indexOf(String(vA[i][column-1]).trim())==-1)
{
uiA.push(vA[i][column-1]);
}
}
return uiA;
}
function emailAddressAssociatedWithEachClient()
{
var agentEmailsA=uniqueItemArray(4);
var clientA=uniqueItemArray(5);
var ss=SpreadsheetApp.getActive();
var cntSh=ss.getSheetByName('Count');
var cntRg=cntSh.getDataRange();
var vA=cntRg.getValues();
var cEO=[];
for(var i=0;i<clientA.length;i++)
{
var ceA=[];
for(var j=2;j<vA.length;j++)
{
if(ceA.indexOf(String(vA[j][3]).trim())==-1 && String(vA[j][4]).trim()==clientA[i])
{
ceA.push(vA[j][3]);
}
}
cEO[clientA[i]]=ceA;
}
/*
for(var n=0;n<clientA.length;n++)
{
Logger.log(clientA[n] + '\n' + cEO[clientA[n]]);
}
*/
return cEO;
}
function clientsAssociatedWithEachEmail()
{
var agentEmailsA=uniqueItemArray(4);
var clientA=uniqueItemArray(5);
var ss=SpreadsheetApp.getActive();
var cntSh=ss.getSheetByName('Count');
var cntRg=cntSh.getDataRange();
var vA=cntRg.getValues();
var cEO=[];
for(var i=0;i<agentEmailsA.length;i++)
{
var ceA=[];
for(var j=2;j<vA.length;j++)
{
if(ceA.indexOf(String(vA[j][4]).trim())==-1 && String(vA[j][3]).trim()==agentEmailsA[i])
{
ceA.push(vA[j][4]);
}
}
cEO[agentEmailsA[i]]=ceA;
}
/*
for(var n=0;n<agentEmailsA.length;n++)
{
Logger.log(agentEmailsA[n] + '\n' + cEO[agentEmailsA[n]]);
}
*/
return cEO;
}
function callsMadeByAgentTotal()
{
var agentA=uniqueItemArray(4);
var clientA=uniqueItemArray(5);
var ss=SpreadsheetApp.getActive();
var cntSh=ss.getSheetByName('Count');
var cntRg=cntSh.getDataRange();
var vA=cntRg.getValues();
var callsByAgent=[];
for(var i=0;i<agentA.length;i++)
{
var cnt=0;
for(var j=2;j<vA.length;j++)
{
if(vA[j][3]==agentA[i])
{
cnt++;
}
}
callsByAgent[agentA[i]]=cnt;
}
/*
for(var i=0;i<agentA.length;i++)
{
Logger.log('\n%s\n%s',agentA[i],callsByAgent[agentA[i]]);
}
var s=Logger.getLog().replace(/^.+:/gm,'').replace(/\n/g,'<br />');
var userInterface=HtmlService.createHtmlOutput(s);
SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Calls By Agent')
*/
return callsByAgent;
}
function callsMadeByAgentToClient()
{
var agentA=uniqueItemArray(4);
var clientA=uniqueItemArray(5);
var ss=SpreadsheetApp.getActive();
var cntSh=ss.getSheetByName('Count');
var cntRg=cntSh.getDataRange();
var vA=cntRg.getValues();
var callsByAgentToClient=[];
for(var i=0;i<agentA.length;i++)
{
callsByAgentToClient[agentA[i]]=[];
for(var k=0;k<clientA.length;k++)
{
var cnt=0;
for(var j=2;j<vA.length;j++)
{
if(vA[j][3]==agentA[i] && vA[j][4]==clientA[k])
{
cnt++;
}
}
callsByAgentToClient[agentA[i]][clientA[k]]=cnt;
}
}
/*
for(var i=0;i<agentA.length;i++)
{
for(var j=0;j<clientA.length;j++)
{
Logger.log('\n%s\n%s\n%s',agentA[i],clientA[j],callsByAgentToClient[agentA[i]][clientA[j]]);
}
}
var s=Logger.getLog().replace(/^.+:/gm,'').replace(/\n/g,'<br />');
var userInterface=HtmlService.createHtmlOutput(s);
SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Calls By Agent To Client')
*/
return callsByAgentToClient;
}
function apptsBookedByAgentTotal()
{
var agentA=uniqueItemArray(4);
var clientA=uniqueItemArray(5);
var ss=SpreadsheetApp.getActive();
var cntSh=ss.getSheetByName('Count');
var cntRg=cntSh.getDataRange();
var vA=cntRg.getValues();
var apptsByAgent=[];
for(var i=0;i<agentA.length;i++)
{
var cnt=0;
for(var j=2;j<vA.length;j++)
{
if(vA[j][3]==agentA[i] && vA[j][5])
{
cnt++;
}
}
apptsByAgent[agentA[i]]=cnt;
}
/*
for(var i=0;i<agentA.length;i++)
{
Logger.log('\n%s\n%s',agentA[i],apptsByAgent[agentA[i]]);
}
var s=Logger.getLog().replace(/^.+:/gm,'').replace(/\n/g,'<br />');
var userInterface=HtmlService.createHtmlOutput(s);
SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Calls By Agent')
*/
return apptsByAgent;
}
function apptsBookedByAgentForClient()
{
var agentA=uniqueItemArray(4);
var clientA=uniqueItemArray(5);
var ss=SpreadsheetApp.getActive();
var cntSh=ss.getSheetByName('Count');
var cntRg=cntSh.getDataRange();
var vA=cntRg.getValues();
var apptsByAgentForClient=[];
for(var i=0;i<agentA.length;i++)
{
apptsByAgentForClient[agentA[i]]=[];
for(var k=0;k<clientA.length;k++)
{
var cnt=0;
for(var j=2;j<vA.length;j++)
{
if(vA[j][3]==agentA[i] && vA[j][4]==clientA[k] && vA[j][5])
{
cnt++;
}
}
apptsByAgentForClient[agentA[i]][clientA[k]]=cnt;
}
}
/*
for(var i=0;i<agentA.length;i++)
{
for(var j=0;j<clientA.length;j++)
{
Logger.log('\n%s\n%s\n%s',agentA[i],clientA[j],apptsByAgentForClient[agentA[i]][clientA[j]]);
}
}
var s=Logger.getLog().replace(/^.+:/gm,'').replace(/\n/g,'<br />');
var userInterface=HtmlService.createHtmlOutput(s);
SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Calls By Agent To Client')
*/
return apptsByAgentForClient;
}
And here's an image of my reports sheet:
Check Out reportSummaryNew() it creates a new sheet generates the report body and fills in all of the data. A separate function called buildReport() does the report building and returns an array of ranges for reportSummaryNew().
function buildReport()
{
var br='<br />';
var agentA=uniqueItemArray(4);
var clientA=uniqueItemArray(5);
var ss=SpreadsheetApp.getActive();
var sh=ss.insertSheet();
var shName=sh.getName();
sh.activate();
var rngA=[];
for(var i=0;i<=clientA.length;i++)
{
rngA[i]=sh.getRange(i*(agentA.length + 2) + 1,1,agentA.length + 2,3);
var vA=rngA[i].getValues();
if(i!=clientA.length)
{
vA[0][0]=clientA[i];
}
else
{
vA[0][0]='All';
}
vA[1][1]='Calls Made';
vA[1][2]='Appointments Booked';
for(var j=0;j<agentA.length;j++)
{
vA[j+2][0]=agentA[j];
}
rngA[i].setValues(vA);
}
return rngA;
}
function reportSummaryNew()
{
var br='<br />';
var agentA=uniqueItemArray(4);
var clientA=uniqueItemArray(5);
var clientAgents=emailAddressAssociatedWithEachClient();
var agentClients=clientsAssociatedWithEachEmail();
var callsByAgent=callsMadeByAgentTotal();
var callsByAgentToClient=callsMadeByAgentToClient();
var apptsByAgent=apptsBookedByAgentTotal();
var apptsByAgentForClient=apptsBookedByAgentForClient();
var rngA=buildReport();
var ss=SpreadsheetApp.getActive();
var rptSh=ss.getActiveSheet();
for(var i=0;i<rngA.length-1;i++)
{
var rg1=rngA[i];
var vA1=rg1.getValues();
vA1[2][1]=callsByAgentToClient[vA1[2][0]][vA1[0][0]];
vA1[3][1]=callsByAgentToClient[vA1[3][0]][vA1[0][0]];
vA1[4][1]=callsByAgentToClient[vA1[4][0]][vA1[0][0]];
vA1[2][2]=apptsByAgentForClient[vA1[2][0]][vA1[0][0]];
vA1[3][2]=apptsByAgentForClient[vA1[3][0]][vA1[0][0]];
vA1[4][2]=apptsByAgentForClient[vA1[4][0]][vA1[0][0]];
rg1.setValues(vA1);
}
var rg2=rngA[rngA.length-1];
var vA2=rg2.getValues();
vA2[2][1]=callsByAgent[vA2[2][0]];
vA2[3][1]=callsByAgent[vA2[3][0]];
vA2[4][1]=callsByAgent[vA2[4][0]];
vA2[2][2]=apptsByAgent[vA2[2][0]];
vA2[3][2]=apptsByAgent[vA2[3][0]];
vA2[4][2]=apptsByAgent[vA2[4][0]];
rg2.setValues(vA2);
}
Here's what the new Generated Report Looks Like:
If you wish to be able to format the report you can run buildReport and take one of the sections and move it to the upper left corner A1:C5 and format it any way you like and then replace this line var rngA=buildReport() in reportSummaryNew() with this var rngA=buildReport1() yes just add a 1 between the 't' and '('. The buildReport1() function will copy the format from Format!A1:A5 so you can change the format easily any time you want just by formatting the contents of the sheet named 'Format';
Here's buildReport1()
function buildReport1()
{
var br='<br />';
var agentA=uniqueItemArray(4);
var clientA=uniqueItemArray(5);
var ss=SpreadsheetApp.getActive();
var trng=ss.getSheetByName('Format').getRange("A1:C5");
var sh=ss.insertSheet();
var shName=sh.getName();
sh.activate();
var rngA=[];
for(var i=0;i<=clientA.length;i++)
{
rngA[i]=sh.getRange(i*(agentA.length + 2) + 1,1,agentA.length + 2,3);
var vA=rngA[i].getValues();
trng.copyTo(rngA[i],{formatOnly:true});
if(i!=clientA.length)
{
vA[0][0]=clientA[i];
}
else
{
vA[0][0]='All';
}
vA[1][1]='Calls Made';
vA[1][2]='Appointments Booked';
for(var j=0;j<agentA.length;j++)
{
vA[j+2][0]=agentA[j];
}
rngA[i].setValues(vA);
}
return rngA;
}
And here's and image of how I formatted it.
I'm want to shorten this function with array
document.mySchedule.breakfast.onchange = function() {
var id = document.mySchedule.breakfast.selectedIndex;
var val = document.mySchedule.breakfast[id].value;
strUser[0]=val;
}
document.mySchedule.lunch.onchange = function() {
var id = document.mySchedule.lunch.selectedIndex;
var val = document.mySchedule.lunch[id].value;
strUser[1]=val;
}
document.mySchedule.dinner.onchange = function() {
var id = document.mySchedule.dinner.selectedIndex;
var val = document.mySchedule.dinner[id].value;
strUser[2]=val;
}
I'm try this way but it didn't seem to be work.
var selectData = ["breakfast","lunch","dinner"]
for(i = 0; i < selectData.lenght; i++) {
document.mySchedule.selectData[i].onchange = function() {
var id = document.mySchedule.selectData[i].selectedIndex;
var val = document.mySchedule.selectData[i][id].value;
strUser[i]=val;
}
}
by the way this use to control selector.
Hope someone can help. thank you
Use:
var selectData = ["breakfast","lunch","dinner"]
for(i = 0; i < selectData.lenght; i++) {
var e = document.getElementById(selectData[i]);
e.onchange = function(){
strUser[i] = e.value;
};
}
When I run the javascript code below, it load specified amount of images from Flickr.
By var photos = photoGroup.getPhotos(10) code, I get 10 images from cache.
Then, I can see the object has exactly 10 items by checking console.log(photos);
But actual image appeared on the page is less than 10 items...
I have no idea why this work this way..
Thank you in advance.
<html>
<head>
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<script>
var PhotoGroup = function(nativePhotos, callback) {
var _cache = new Array();
var numberOfPhotosLoaded = 0;
var containerWidth = $("#contents").css('max-width');
var containerHeight = $("#contents").css('max-height');
$(nativePhotos).each(function(key, photo) {
$("<img src='"+"http://farm" + photo["farm"] + ".staticflickr.com/" + photo["server"] + "/" + photo["id"] + "_" + photo["secret"] + "_b.jpg"+"'/>")
.attr("alt", photo['title'])
.attr("data-cycle-title", photo['ownername'])
.load(function() {
if(this.naturalWidth >= this.naturalHeight) {
$(this).attr("width", containerWidth);
} else {
$(this).attr("height", containerHeight);
}
_cache.push(this);
if(nativePhotos.length == ++numberOfPhotosLoaded)
callback();
})
});
var getRandom = function(max) {
return Math.floor((Math.random()*max)+1);
}
this.getPhotos = function(numberOfPhotos) {
var photoPool = new Array();
var maxRandomNumber = _cache.length-1;
while(photoPool.length != numberOfPhotos) {
var index = getRandom(maxRandomNumber);
if($.inArray(_cache[index], photoPool))
photoPool.push(_cache[index]);
}
return photoPool;
}
}
var Contents = function() {
var self = this;
var contentTypes = ["#slideShowWrapper", "#video"];
var switchTo = function(nameOfContent) {
$(contentTypes).each(function(contentType) {
$(contentType).hide();
});
switch(nameOfContent) {
case("EHTV") :
$("#video").show();
break;
case("slideShow") :
$("#slideShowWrapper").show();
break;
default :
break;
}
}
this.startEHTV = function() {
switchTo("EHTV");
document._video = document.getElementById("video");
document._video.addEventListener("loadstart", function() {
document._video.playbackRate = 0.3;
}, false);
document._video.addEventListener("ended", startSlideShow, false);
document._video.play();
}
this.startSlideShow = function() {
switchTo("slideShow");
var photos = photoGroup.getPhotos(10)
console.log(photos);
$('#slideShow').html(photos);
}
var api_key = '6242dcd053cd0ad8d791edd975217606';
var group_id = '2359176#N25';
var flickerAPI = 'http://api.flickr.com/services/rest/?jsoncallback=?';
var photoGroup;
$.getJSON(flickerAPI, {
api_key: api_key,
group_id: group_id,
format: "json",
method: "flickr.groups.pools.getPhotos",
}).done(function(data) {
photoGroup = new PhotoGroup(data['photos']['photo'], self.startSlideShow);
});
}
var contents = new Contents();
</script>
</head>
<body>
<div id="slideShow"></div>
</body>
</html>
I fix your method getRandom() according to this article, and completely re-write method getPhotos():
this.getPhotos = function(numberOfPhotos) {
var available = _cache.length;
if (numberOfPhotos >= available) {
// just clone existing array
return _cache.slice(0);
}
var result = [];
var indices = [];
while (result.length != numberOfPhotos) {
var r = getRandom(available);
if ($.inArray(r, indices) == -1) {
indices.push(r);
result.push(_cache[r]);
}
}
return result;
}
Check full solution here: http://jsfiddle.net/JtDzZ/
But this method still slow, because loop may be quite long to execute due to same random numbers occurred.
If you care about performance, you need to create other stable solution. For ex., randomize only first index of your images sequence.
Hello Auto completion is not working well in my application.When we type a name it displays only a blank list[ Screenshots attached ].
Controller Code
public function list_UserByName($letters)
{
if(strpos($letters, ","))
{
$letters1 = explode(",",$letters);
$lecount = count($letters1);
$letters = $letters1[$lecount-1];
}
$letters = preg_replace("/[^a-z0-9 ]/si","",$letters);
$response=$this->user_model->getAutoUserList($letters);
}
Model Code
public function getAutoUserList($letters)
{
$letters = preg_replace("/[^a-z0-9 ]/si","",$letters);
//AND user_type='C' AND user_status='A'
$query="select * from gm_users where uname Like '%$letters%'";
$result_query =$this->db->query($query);
foreach($result_query->result() as $result)
{
//echo "###".$result."|";
//$pinlevel =$this->functions->get_pinlevel($result->pinLevel);
//echo $result->userId."###".$result->uname." [ ".$pinlevel." ] "."|";
echo $result->userId."###".$result->uname."".$result->address." ".$result->city."|";
}
}
billing.php
<input type="text" autocomplete="off" size="20" name="txtname" id="txtname" onkeyup="ajax_showOptions(this,'getCountriesByLetters',event);" value=""/>
ajax-dynamic-list.js
/************************************************************************************************************
(C) www.dhtmlgoodies.com, April 2006
This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.
Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.
Thank you!
www.dhtmlgoodies.com
Alf Magne Kalleland
************************************************************************************************************/
var ajaxBox_offsetX = 25;
var ajaxBox_offsetY = 5;
var ajax_list_externalFile = site_url+'/catalog/list_UserByName'; // Path to external file
var minimumLettersBeforeLookup = 1; // Number of letters entered before a lookup is performed.
var ajax_list_objects = new Array();
var ajax_list_cachedLists = new Array();
var ajax_list_activeInput = false;
var ajax_list_activeItem;
var ajax_list_optionDivFirstItem = false;
var ajax_list_currentLetters = new Array();
var ajax_optionDiv = false;
var ajax_optionDiv_iframe = false;
var ajax_list_MSIE = false;
if(navigator.userAgent.indexOf('MSIE')>=0 && navigator.userAgent.indexOf('Opera')<0)ajax_list_MSIE=true;
var currentListIndex = 0;
function ajax_getTopPos(inputObj)
{
var returnValue = inputObj.offsetTop;
while((inputObj = inputObj.offsetParent) != null){
returnValue += inputObj.offsetTop;
}
return returnValue;
}
function ajax_list_cancelEvent()
{
return false;
}
function ajax_getLeftPos(inputObj)
{
var returnValue = inputObj.offsetLeft;
while((inputObj = inputObj.offsetParent) != null)returnValue += inputObj.offsetLeft;
return returnValue;
}
// Edited
function ajax_option_setValue_bkp(e,inputObj)
{
if(!inputObj)inputObj=this;
var tmpValue = inputObj.innerHTML;
//alert(inputObj.id);
document.getElementById('saleUserId').value=inputObj.id;
if(ajax_list_MSIE)tmpValue = inputObj.innerText;else tmpValue = inputObj.textContent;
if(!tmpValue)tmpValue = inputObj.innerHTML;
val = ajax_list_activeInput.value.split(',');
vals = '';
count = val.length - 1;
for(i=0;i<count;i++)
{
vals = vals + val[i] + ',';
}
ajax_list_activeInput.value = vals + tmpValue;
if(document.getElementById(ajax_list_activeInput.name + '_hidden'))document.getElementById(ajax_list_activeInput.name + '_hidden').value = inputObj.id;
ajax_options_hide();
}
function ajax_option_setValue(e,inputObj)
{
if(!inputObj)inputObj=this;
var tmpValue = inputObj.innerHTML;
//alert(inputObj.id);
document.getElementById('saleUserId').value=inputObj.id;
if(ajax_list_MSIE)tmpValue = inputObj.innerText;else tmpValue = inputObj.textContent;
if(!tmpValue)tmpValue = inputObj.innerHTML;
ajax_list_activeInput.value = tmpValue;
if(document.getElementById(ajax_list_activeInput.name + '_hidden'))document.getElementById(ajax_list_activeInput.name + '_hidden').value = inputObj.id;
ajax_options_hide();
}
function ajax_options_hide()
{
if(ajax_optionDiv)ajax_optionDiv.style.display='none';
if(ajax_optionDiv_iframe)ajax_optionDiv_iframe.style.display='none';
}
function ajax_options_rollOverActiveItem(item,fromKeyBoard)
{
if(ajax_list_activeItem)ajax_list_activeItem.className='optionDiv';
item.className='optionDivSelected';
ajax_list_activeItem = item;
if(fromKeyBoard){
if(ajax_list_activeItem.offsetTop>ajax_optionDiv.offsetHeight){
ajax_optionDiv.scrollTop = ajax_list_activeItem.offsetTop - ajax_optionDiv.offsetHeight + ajax_list_activeItem.offsetHeight + 2 ;
}
if(ajax_list_activeItem.offsetTop<ajax_optionDiv.scrollTop)
{
ajax_optionDiv.scrollTop = 0;
}
}
}
function ajax_option_list_buildList(letters,paramToExternalFile)
{
ajax_optionDiv.innerHTML = '';
ajax_list_activeItem = false;
if(ajax_list_cachedLists[paramToExternalFile][letters.toLowerCase()].length<=1){
ajax_options_hide();
return;
}
ajax_list_optionDivFirstItem = false;
var optionsAdded = false;
for(var no=0;no<ajax_list_cachedLists[paramToExternalFile][letters.toLowerCase()].length;no++){
if(ajax_list_cachedLists[paramToExternalFile][letters.toLowerCase()][no].length==0)continue;
optionsAdded = true;
var div = document.createElement('DIV');
var items = ajax_list_cachedLists[paramToExternalFile][letters.toLowerCase()][no].split(/###/gi);
if(ajax_list_cachedLists[paramToExternalFile][letters.toLowerCase()].length==1 && ajax_list_activeInput.value == items[0]){
ajax_options_hide();
return;
}
div.innerHTML = items[items.length-1];
div.id = items[0];
div.className='optionDiv';
div.onmouseover = function(){ ajax_options_rollOverActiveItem(this,false) }
div.onclick = ajax_option_setValue;
if(!ajax_list_optionDivFirstItem)ajax_list_optionDivFirstItem = div;
ajax_optionDiv.appendChild(div);
}
if(optionsAdded){
ajax_optionDiv.style.display='block';
if(ajax_optionDiv_iframe)ajax_optionDiv_iframe.style.display='';
ajax_options_rollOverActiveItem(ajax_list_optionDivFirstItem,true);
}
}
function ajax_option_list_showContent(ajaxIndex,inputObj,paramToExternalFile,whichIndex)
{
if(whichIndex!=currentListIndex)return;
var letters = inputObj.value;
var content = ajax_list_objects[ajaxIndex].response;
var elements = content.split('|');
//alert(content);
ajax_list_cachedLists[paramToExternalFile][letters.toLowerCase()] = elements;
ajax_option_list_buildList(letters,paramToExternalFile);
}
function ajax_option_resize(inputObj)
{
ajax_optionDiv.style.top = (ajax_getTopPos(inputObj) + inputObj.offsetHeight + ajaxBox_offsetY) + 'px';
ajax_optionDiv.style.left = (ajax_getLeftPos(inputObj) + ajaxBox_offsetX) + 'px';
if(ajax_optionDiv_iframe){
ajax_optionDiv_iframe.style.left = ajax_optionDiv.style.left;
ajax_optionDiv_iframe.style.top = ajax_optionDiv.style.top;
}
}
function ajax_showOptions(inputObj,paramToExternalFile,e)
{
document.getElementById('saleUserId').value='';
if(e.keyCode==13 || e.keyCode==9)return;
if(ajax_list_currentLetters[inputObj.name]==inputObj.value)return;
if(!ajax_list_cachedLists[paramToExternalFile])ajax_list_cachedLists[paramToExternalFile] = new Array();
ajax_list_currentLetters[inputObj.name] = inputObj.value;
if(!ajax_optionDiv){
ajax_optionDiv = document.createElement('DIV');
ajax_optionDiv.id = 'ajax_listOfOptions';
document.body.appendChild(ajax_optionDiv);
if(ajax_list_MSIE){
ajax_optionDiv_iframe = document.createElement('IFRAME');
ajax_optionDiv_iframe.border='0';
ajax_optionDiv_iframe.style.width = ajax_optionDiv.clientWidth + 'px';
ajax_optionDiv_iframe.style.height = ajax_optionDiv.clientHeight + 'px';
ajax_optionDiv_iframe.id = 'ajax_listOfOptions_iframe';
document.body.appendChild(ajax_optionDiv_iframe);
}
var allInputs = document.getElementsByTagName('INPUT');
for(var no=0;no<allInputs.length;no++){
if(!allInputs[no].onkeyup)allInputs[no].onfocus = ajax_options_hide;
}
var allSelects = document.getElementsByTagName('SELECT');
for(var no=0;no<allSelects.length;no++){
allSelects[no].onfocus = ajax_options_hide;
}
var oldonkeydown=document.body.onkeydown;
if(typeof oldonkeydown!='function'){
document.body.onkeydown=ajax_option_keyNavigation;
}else{
document.body.onkeydown=function(){
oldonkeydown();
ajax_option_keyNavigation() ;}
}
var oldonresize=document.body.onresize;
if(typeof oldonresize!='function'){
document.body.onresize=function() {ajax_option_resize(inputObj); };
}else{
document.body.onresize=function(){oldonresize();
ajax_option_resize(inputObj) ;}
}
}
if(inputObj.value.length<minimumLettersBeforeLookup){
ajax_options_hide();
return;
}
ajax_optionDiv.style.top = (ajax_getTopPos(inputObj) + inputObj.offsetHeight + ajaxBox_offsetY) + 'px';
ajax_optionDiv.style.left = (ajax_getLeftPos(inputObj) + ajaxBox_offsetX) + 'px';
if(ajax_optionDiv_iframe){
ajax_optionDiv_iframe.style.left = ajax_optionDiv.style.left;
ajax_optionDiv_iframe.style.top = ajax_optionDiv.style.top;
}
ajax_list_activeInput = inputObj;
ajax_optionDiv.onselectstart = ajax_list_cancelEvent;
currentListIndex++;
if(ajax_list_cachedLists[paramToExternalFile][inputObj.value.toLowerCase()]){
ajax_option_list_buildList(inputObj.value,paramToExternalFile,currentListIndex);
}else{
var tmpIndex=currentListIndex/1;
ajax_optionDiv.innerHTML = '';
var ajaxIndex = ajax_list_objects.length;
ajax_list_objects[ajaxIndex] = new sack();
var search_key = inputObj.value.replace(" ","+");
//search_key1 = search_key.replace(",",",");
var url = ajax_list_externalFile + '/' +search_key;
ajax_list_objects[ajaxIndex].requestFile = url; // Specifying which file to get
ajax_list_objects[ajaxIndex].onCompletion = function(){ ajax_option_list_showContent(ajaxIndex,inputObj,paramToExternalFile,tmpIndex); }; // Specify function that will be executed after file has been found
ajax_list_objects[ajaxIndex].runAJAX(); // Execute AJAX function
}
}
function wordcount(string) {
var a = string.split(/\s+/g); // split the sentence into an array of words
return a.length;
}
function ajax_option_keyNavigation(e)
{
if(document.all)e = event;
if(!ajax_optionDiv)return;
if(ajax_optionDiv.style.display=='none')return;
if(e.keyCode==38){ // Up arrow
if(!ajax_list_activeItem)return;
if(ajax_list_activeItem && !ajax_list_activeItem.previousSibling)return;
ajax_options_rollOverActiveItem(ajax_list_activeItem.previousSibling,true);
}
if(e.keyCode==40){ // Down arrow
if(!ajax_list_activeItem){
ajax_options_rollOverActiveItem(ajax_list_optionDivFirstItem,true);
}else{
if(!ajax_list_activeItem.nextSibling)return;
ajax_options_rollOverActiveItem(ajax_list_activeItem.nextSibling,true);
}
}
/*if(e.keyCode==13 || e.keyCode==9){ // Enter key or tab key
if(ajax_list_activeItem && ajax_list_activeItem.className=='optionDivSelected')ajax_option_setValue(false,ajax_list_activeItem);
if(e.keyCode==13)return false; else return true;
}
if(e.keyCode==27){ // Escape key
ajax_options_hide();
}*/
}
//document.documentElement.onclick = autoHideList;
function autoHideList(e)
{
if(document.all)e = event;
if (e.target) source = e.target;
else if (e.srcElement) source = e.srcElement;
if (source.nodeType == 3) // defeat Safari bug
source = source.parentNode;
if(source.tagName.toLowerCase()!='input' && source.tagName.toLowerCase()!='textarea')ajax_options_hide();
}
Am a beginner in php as well as Codeigniter
Just echo your data in your controller
change
$response=$this->user_model->getAutoUserList($letters);
To
echo $this->user_model->getAutoUserList($letters);
change
onkeyup="ajax_showOptions(this,'getCountriesByLetters',event);
to
onkeyup="ajax_showOptions(this,'list_UserByName',event);
there is a question on this topic on stackoverflow, but an entire different process.
My Codeigniter autocomplete with ajax