Results 1 to 4 of 4

Thread: Help me to understand these codes!?

  1. #1
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help me to understand these codes!?

    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.


    Code:
    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;
       
    
    }

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Code:
    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;
       
    
    }
    Jeremy | jfein.net

  3. #3
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you so much

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    No problem, I'm glad to help

    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"
    Jeremy | jfein.net

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •