PDA

View Full Version : Help me to understand these codes!?



newbe123
12-08-2010, 05:17 PM
I'm totally new when it comes to JavaScript and I'm trying to learn new things. Here I found the codes I tried it and it works but I would like to have comments for the code to understand how they have proceeded.
can someone help me and explain these codes with comments?:confused:
I really appreciate it.




var config = {
"tags": "vfx+compositing",
"user": "andreasl",
"scriptTagTarget": "scriptTagDiv",
"deliciousTarget": "deliciousLinks",
"callbackFunction": "fetchDelicious"
};

window.onload = function() {
var url = 'http://feeds.delicious.com/v2/json/' + config.user + '/' + config.tags + '?callback=' + config.callbackFunction;
var scriptDiv = document.getElementById(config.scriptTagTarget);
addScriptTag(url, scriptDiv);
};



function addScriptTag(url, scriptDiv) {

if (scriptDiv.hasChildNodes())
{
scriptDiv.removeChild(scriptDiv.firstChild)
};

var scriptElement = document.createElement('script');
scriptElement.setAttribute('src', url);
scriptDiv.appendChild(scriptElement);
}


function fetchDelicious(json) {
var html = "";
for (var i = 0; i < json.length; i++) {
var uri = json[i].u;
var description = json[i].d;
var tags = json[i].t; //array
var time = json[i].dt;
//var n = json[i].n; //new?
var author = json[i].a;
var bHtml = "<li><a href=\":u\">:d</a>:t</li>".replace(":u", uri).replace(":d", description);
var tagHtml = "";
for(var n = 0; n < tags.length; n++) {
tagHtml += "<li><a href=\"http://delicious.com/:u\">:d</a></li>".replace(":u", [author,tags[n]].join("/")).replace(":d", tags[n]);
}
tagHtml = "<ul>" + tagHtml + "</ul>";
html += bHtml.replace(":t", tagHtml);
}
html = "<ul>" + html + "</ul>";
document.getElementById(config.deliciousTarget).innerHTML = html;


}

Nile
12-09-2010, 12:39 AM
var config = {
"tags": "vfx+compositing",
"user": "andreasl",
"scriptTagTarget": "scriptTagDiv",
"deliciousTarget": "deliciousLinks",
"callbackFunction": "fetchDelicious"
}; //array, tags = vfx+compositing, user = andreasl, scriptTagTarget = scriptTagDiv, etc..

window.onload = function() { //when the page loads
var url = 'http://feeds.delicious.com/v2/json/' + config.user + '/' + config.tags + '?callback=' + config.callbackFunction; //set a variable to the value of feeds.delicious.com/v2/json/user/tags?callback=callbackFunction
var scriptDiv = document.getElementById(config.scriptTagTarget); //get the target div 'scriptTagDiv'
addScriptTag(url, scriptDiv); //custom function addScripTag with url and scriptdiv - so I'm assuming its put something from the url int he variable url and display it in script div
};



function addScriptTag(url, scriptDiv) { //custom function

if (scriptDiv.hasChildNodes())
{
scriptDiv.removeChild(scriptDiv.firstChild) //if it has children, get rid of the first one
};

var scriptElement = document.createElement('script'); //make a script tag
scriptElement.setAttribute('src', url); //set the src = url(on delicious)
scriptDiv.appendChild(scriptElement); //add the <script type="text/javascript" url="feeds.delicious.com/v2/etc" /> to your page, so the script is applied to your page
}


function fetchDelicious(json) { //custom function using json probably
var html = "";
for (var i = 0; i < json.length; i++) { //the following is getting things such as the descritiptopn, tags, and replacing some things from the javascript file and displaying them on the page
var uri = json[i].u;
var description = json[i].d;
var tags = json[i].t; //array
var time = json[i].dt;
//var n = json[i].n; //new?
var author = json[i].a;
var bHtml = "<li><a href=\":u\">:d</a>:t</li>".replace(":u", uri).replace(":d", description);
var tagHtml = "";
for(var n = 0; n < tags.length; n++) {
tagHtml += "<li><a href=\"http://delicious.com/:u\">:d</a></li>".replace(":u", [author,tags[n]].join("/")).replace(":d", tags[n]);
}
tagHtml = "<ul>" + tagHtml + "</ul>";
html += bHtml.replace(":t", tagHtml);
}
html = "<ul>" + html + "</ul>";
document.getElementById(config.deliciousTarget).innerHTML = html;


}

newbe123
12-09-2010, 01:16 AM
Thank you so much:)

Nile
12-09-2010, 12:52 PM
No problem, I'm glad to help :D

Here on DD, we like to keep things organized. In an effort to do so, you have the option to set a thread to resolved when an issue is fixed. To make the status of the thread resolved:
1. Go to your first post
2. Edit your first post
3. Click "Go Advanced"
4. In the dropdown next to the title, select "RESOLVED"