...

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



newbe
12-08-2010, 06:08 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?
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;


}

Philip M
12-08-2010, 06:21 PM
I'm totally new when it comes to JavaScript and I'm trying to learn new things.

That is admirable, but you are trying to run (or fly!!) before you can walk. It would take a very long time to explain in full this advanced code to someone who is "totally new" to the subject. Suggest you master more basic things first.

There were lots of examples of poor defending, both prior to and preceding the goal. - Football Commentator

newbe
12-08-2010, 06:33 PM
I understand what you mean and I have done some but in this code I would like to have comments for the last bit. I have some problems and understand how it has been done and have some explanations and comments.
I really appreciate the help



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;


}

newbe
12-09-2010, 02:55 AM
problem solved:):thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum