-1

i just want to know how to execute this [JSCODE][1] on page load, I'm a newbie and I cant figure it out. I just want to disregard the form or submit button and execute the script on page load. Thank You in advance!

 [1]: http://jsfiddle.net/Noumenon72/9X3yZ/8/
  • 1
    Welcome to Stack Overflow! Please take the [tour], have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Any code related to your question must be **in** your question, not just linked. Links rot, making the question and its answers useless to people in the future, and people shouldn't have to go off-site to help you. If the question doesn't make sense and can't be answered without the link, it's not appropriate for this site. Instead, put the [**minimum** complete example](/help/mcve) in the question. – T.J. Crowder Feb 10 '16 at 07:31

2 Answers2

0

Write your code inside anonymous function given below..

$(function() {
    //Write your code here
})
Vikas
  • 39
  • 11
  • thank you sir, I tried this on my blogger page
    but the list doesnt have a url link when i test it
    – Darren Darryl Feb 10 '16 at 07:39
0

Use jquery $(document).ready like this.

$(document).ready(function(){
    //task which you want to perform
});

See you code below. I have mentioned where to call these functions.

        $(document).ready(function(){
      $('#domain').val('http://yourblog.blogspot.com/');
      $('#get_tags').click();
    });


    function getTagsFromFeed(domain){
        var myscript = document.createElement("script");
        myscript.src = domain + "feeds/posts/summary?alt=json&max-results=0&callback=cat";
        document.getElementsByTagName('head')[0].appendChild(myscript);
    }

    function cat(json){ //get categories of blog & sort them
            var label = json.feed.category;
            var lst=[];
            for (i=0; i<label.length; i++){
              lst[i] = label[i].term;  
            }
            displayList(lst.sort());  //use any sort if you need that 
        }

    function displayList(list) {
        var mylist = document.getElementById("mylist");
        mylist.innerHTML = "";
        for (i=0; i<list.length; i++) {
            var li = document.createElement("li");
            li.appendChild(document.createTextNode(list[i]));
            mylist.appendChild(li);
        }
        urlifyTagsInList(document.forms.myform.host.value);
    }

    function urlifyTagsInList(hostname){
        var mylist = document.getElementById("mylist");
        var newlist = document.createElement("ul");
        var elements = mylist.getElementsByTagName("li");
        for (j=0; j<elements.length; j++) {
            var link = document.createElement("a");
            var blah = document.createTextNode("blah");
            link.href=hostname + "search/label/" + elements[j].innerHTML;
            link.appendChild(elements[j].cloneNode(true));
            newlist.appendChild(link);
        }
        mylist.parentNode.replaceChild(newlist, mylist);
        newlist.id = "mylist";
    }
    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://code.jquery.com/jquery-2.1.4.js"></script>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>JS Bin</title>
    </head>
    <body>
      <form id="myform" method="POST" onSubmit="getTagsFromFeed(document.forms.myform.host.value); return false;">
        <p>  Enter blogspot domain (http://yourblog.blogspot.com/):</p>
        <input id="domain" type="text" name="host"></input>
        <button id="get_tags" type="submit">Get tags</button>
    </form>
    <ul id="mylist">
      
      
    </body>
    </html>

If you want to use pure javascript, Document ready with pure JavaScript will help you.

A simple way to submit form onload is like this.

$(document).ready(function(){
    $('#myForm').submit();
});
Community
  • 1
  • 1
Muhammad Hassan
  • 14,086
  • 7
  • 32
  • 54