Jump to content

Help me to understand these codes!?


newbe123

Recommended Posts

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\"></a>:t</li>".replace(":u", uri).replace("", description); 
	var tagHtml = ""; 
	for(var n = 0; n < tags.length; n++)  { 
	tagHtml += "<li><a href=\"http://delicious.com/:u\"></a></li>".replace(":u", [author,tags[n]].join("/")).replace("", tags[n]); 
	}
	tagHtml = "<ul>" + tagHtml + "</ul>";
	html += bHtml.replace(":t", tagHtml); 
}
html = "<ul>" + html + "</ul>"; 
document.getElementById(config.deliciousTarget).innerHTML = html;
   

}


Link to comment
Share on other sites

It is this last bit that I would like some help with. I really appreciate if someone could help me and write some comments for these codes.

I would really 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\"></a>:t</li>".replace(":u", uri).replace("", description);
      var tagHtml = "";
      for(var n = 0; n < tags.length; n++)  {
      tagHtml += "<li><a href=\"http://delicious.com/:u\"></a></li>".replace(":u", [author,tags[n]].join("/")).replace("", tags[n]);
      }
      tagHtml = "<ul>" + tagHtml + "</ul>";
      html += bHtml.replace(":t", tagHtml);
   }
   html = "<ul>" + html + "</ul>";
   document.getElementById(config.deliciousTarget).innerHTML = html;
   

}

Link to comment
Share on other sites

This is how I understand the codes, do not know if I am right or not?

Can someone help me and see if my comments are right?

 

function fetchDelicious(json) {

var html = ""; / / Create variable html

/ / Go through all the links json pick up and populate the list

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 author = json[i].a;

/ / (li do not seem to be used to create 'html', removed)

	var bHtml = "<li><a href=\":u\"></a>:t</li>".replace(":u", uri).replace("", description); 
/ / Li tags with links placed in the variable bHtml 

	var tagHtml = ""; / / TagHtml harboring tags / bookmarks for each link

/ / Go through all the tags / bookmarks, and populate the level-two-list
	for(var n = 0; n < tags.length; n++)  { 
	tagHtml += "<li><a href=\"http://delicious.com/:u\"></a></li>".replace(":u", [author,tags[n]].join("/")).replace("", tags[n]); 
	}
	tagHtml = "<ul>" + tagHtml + "</ul>"; 
	html += bHtml.replace(":t", tagHtml); / / List is created with the li-tags that contain new ul-list of tags / bookmarks
}
html = "<ul>" + html + "</ul>"; 
document.getElementById(config.deliciousTarget).innerHTML = html;
   

}

Link to comment
Share on other sites

fetchDelicious() takes a JSON encoded string and transforms it to a list in html. The .replace(':u', ..) and likes seem overhead IMO.

 

var config = {};

 

Creates an object literal like [] creates an array literal.

 

window.onload = function() {};

 

Attaches a function to execute upon window has been loaded. The function used is a closure: any variables that exists within it's parent scope are automatically copied into the function's scope.

 

var helloWorld = 'Hello!';
var hello = function() { alert(helloWorld); };

hello();

Link to comment
Share on other sites

I really appreciate your help but I wonder if you could take a look at the codes below and see if my comments are right and what I've missed perhaps? Thank u so much

 

 

function fetchDelicious(json) {

   var html = ""; / / Create variable html

   / / Go through all the links json pick up and populate the list

   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 author = json[i].a;

/ / (li do not seem to be used to create 'html', removed)

      var bHtml = "<li><a href=\":u\"></a>:t</li>".replace(":u", uri).replace("", description);
/ / Li tags with links placed in the variable bHtml

      var tagHtml = ""; / / TagHtml harboring tags / bookmarks for each link

/ / Go through all the tags / bookmarks, and populate the level-two-list
      for(var n = 0; n < tags.length; n++)  {
      tagHtml += "<li><a href=\"http://delicious.com/:u\"></a></li>".replace(":u", [author,tags[n]].join("/")).replace("", tags[n]);
      }
      tagHtml = "<ul>" + tagHtml + "</ul>";
      html += bHtml.replace(":t", tagHtml); / / List is created with the li-tags that contain new ul-list of tags / bookmarks
   }
   html = "<ul>" + html + "</ul>";
   document.getElementById(config.deliciousTarget).innerHTML = html;
   

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.