www.webdeveloper.com
Results 1 to 15 of 15

Thread: I'm a Javascript beginner, please help!!!

  1. #1
    Join Date
    Nov 2011
    Posts
    7

    I'm a Javascript beginner, please help!!!

    Hello, first off I am Very new at Javascript so please be patient with me

    I want to use the values of text boxes on my HTML webpage to create a webpage URL (like below):

    <script type="text/javascript">
    function GetPage()
    {
    document.getElementById('ID1').innerHTML = "http://www.website.com" + document.form.txt1.value + "/" + document.form.txt2.value + "/" + document.form.txt3.value + "home.html";
    }
    </script>

    My text boxes are as follows:

    <form name="form">
    <td>Person: <input type="text" name="txt1" size="12"></td>
    <td>Place: <input type="text" name="txt1" size="12"></td>
    <td>Thing: <input type="text" name="txt1" size="12"></td>
    <td width="100"></td>
    <td><input type="button" value="GO" onClick="GetPage()"></td>
    </div>

    Now this all works and the result webpage URL prints to id='ID1', but the big question is how do I use this resulting URL in another Javascript section as the src="?

    For example:

    <script src=WHATEVER WAS RETURNED TO ID NAVIGATION>
    </script>

  2. #2
    Join Date
    May 2003
    Location
    Milwaukee
    Posts
    344
    maybe try something like this:

    Code:
    <script type="text/javascript">
    
    var myurl;
    
    function GetPage()
    {
    myurl = "http://www.website.com" + document.form.txt1.value + "/" + document.form.txt2.value + "/" + document.form.txt3.value + "home.html";
    
    document.getElementById('ID1').innerHTML = myurl;
    }
    </script>

  3. #3
    Join Date
    Nov 2011
    Posts
    7
    Thank you. So would I refer to that global variable like this?

    <script src="myURL">
    </script>

  4. #4
    Join Date
    Nov 2011
    Posts
    7
    Sorry, case was wrong.

    <script src="myurl">
    </script>

  5. #5
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    Arrow

    Quote Originally Posted by Joshua Leonard View Post
    Thank you. So would I refer to that global variable like this?

    <script src="myURL">
    </script>
    Quote Originally Posted by Joshua Leonard View Post
    Sorry, case was wrong.

    <script src="myurl">
    </script>
    Nope, neither will work that way.

  6. #6
    Join Date
    Nov 2011
    Posts
    7
    Thanks for your quick response.

    Is there a way to do it then? All I need to do is pass the results url to the src of the next script.

  7. #7
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    Question

    Quote Originally Posted by Joshua Leonard View Post
    Thanks for your quick response.

    Is there a way to do it then? All I need to do is pass the results url to the src of the next script.
    By "the next script" do you mean another function on the same page?

    Or, do you mean to another URL page altogether?

    Google "javascript querystring" if the latter.

  8. #8
    Join Date
    Nov 2011
    Posts
    7
    Another script on the same page:

    <script type="text/javascript">
    function urldata(obj) {
    //blank on purpose
    //this basically prints out data from another script (which resides on myurl)
    }
    </script>
    <script src="myurl">
    </script>

    So from this, basically (and I don't know how because I found this script online) the urldata function uses the data from myurl to return a result from function urldata.

  9. #9
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    Lightbulb

    Without seeing the scripts that you are talking about,
    this is just a guess as to what your problem is...
    Code:
    <script type="text/javascript">
    function myurl() {
      var str = 'Data from your function';
      return str;
    }
    </script>
    
    <script type="text/javascript">
    function urldata(obj) {
    //blank on purpose
    //this basically prints out data from another script (which resides on myurl)
      alert(obj);
    } 
    
    // test
      var tmp = myurl();
      urldata(tmp);
    </script>
    There is no reason for 2 <script type="text/javascript"> </script> tag pairs.
    You only need the very first and very last tag.

  10. #10
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    This reminds me of that joke about copper wire being invented in Glasgow, Scotland in 1742 when Two jocks were witnessed fighting over a penny.

    Getting to the erroneous code is often like that scenario a fight... or if you prefer the dentist scenario, teeth come out easier... Blood and Stone comes to mind.

    What he needs to do is have an object created with the attributes required and then appending to the head of the document.

    Code:
    var scriptSrc = "path/to/file.js";
    var scriptEle = document.createElement("script");  // a new <script> HTML tag
    scriptEle.setAttribute("src", scriptSrc );          // src 
    scriptEle.setAttribute("type", "text/javascript"); // type="text/javascript"
    var head = document.getElementsByTagName("head")[0];   // the <head>
    head.appendChild(scriptEle);  // append to head
    Should do the trick.
    We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...
    Code:
    Youre = {
          STILL_not_getting_it:function(){
               alert("YOU, the original poster / thread starter NEED to POST the code and NOT a LINK.");
          },
          MissingThePoint:function(msg){
                alert("You're missing the point. " + msg);
          }
    }
    Youre.STILL_not_getting_it();

  11. #11
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    Question

    Quote Originally Posted by JunkMale View Post
    ...
    What he needs to do is have an object created with the attributes required and then appending to the head of the document.

    Code:
    var scriptSrc = "path/to/file.js";
    var scriptEle = document.createElement("script");  // a new <script> HTML tag
    scriptEle.setAttribute("src", scriptSrc );          // src 
    scriptEle.setAttribute("type", "text/javascript"); // type="text/javascript"
    var head = document.getElementsByTagName("head")[0];   // the <head>
    head.appendChild(scriptEle);  // append to head
    Should do the trick.
    The only time I have use that bit of code in the past was if I did not want to load a lengthy script unless there was an option to use it. I would leave the loading of the script out until the user's option selection required it to be loaded.

    What benefit would that bit of code have over just loading the 'file.js' directly in the <head> section since the OP seems to be wanting to use it all the time?

  12. #12
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    I wouldn't know but I guess that the guy has some reason to back door a script.
    We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...
    Code:
    Youre = {
          STILL_not_getting_it:function(){
               alert("YOU, the original poster / thread starter NEED to POST the code and NOT a LINK.");
          },
          MissingThePoint:function(msg){
                alert("You're missing the point. " + msg);
          }
    }
    Youre.STILL_not_getting_it();

  13. #13
    Join Date
    Nov 2011
    Posts
    7
    Quote Originally Posted by JMRKER View Post
    Without seeing the scripts that you are talking about,
    this is just a guess as to what your problem is...
    Code:
    <script type="text/javascript">
    function myurl() {
      var str = 'Data from your function';
      return str;
    }
    </script>
    
    <script type="text/javascript">
    function urldata(obj) {
    //blank on purpose
    //this basically prints out data from another script (which resides on myurl)
      alert(obj);
    } 
    
    // test
      var tmp = myurl();
      urldata(tmp);
    </script>
    There is no reason for 2 <script type="text/javascript"> </script> tag pairs.
    You only need the very first and very last tag.
    Ok thanks...now if you can help me understand that I might be able to make this work (right now it isn't).

    First question, how does functionurldata(obj) actually run? it seems to just run but other functions that are like test() have to be actually called...do they not? ie. onClick="myurl()"

    Why is var tmp = myurl(); urdata(tmp); after the function? Is that just how scripts work?

    So myurl() spits out a string via return str;, then this is turned into a variable called tmp which in turn is injected into urldata in replace of obj?

    Sorry if I'm asking too many questions...need to understand it so I can make it work. Really appreciate your help/

  14. #14
    Join Date
    Nov 2011
    Posts
    7
    Quote Originally Posted by JMRKER View Post
    The only time I have use that bit of code in the past was if I did not want to load a lengthy script unless there was an option to use it. I would leave the loading of the script out until the user's option selection required it to be loaded.

    What benefit would that bit of code have over just loading the 'file.js' directly in the <head> section since the OP seems to be wanting to use it all the time?
    I'm getting other 'script' from Twitter JSON (not someone else's .js script file, haha). I've already figured out how to gather the data I want from the JSON result but basically I want the Twitter API to return a JSON result dependent on the search criteria entered on my site...hence the incorporating of text boxes into the URL.

  15. #15
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    Arrow

    Quote Originally Posted by Joshua Leonard View Post
    Ok thanks...now if you can help me understand that I might be able to make this work (right now it isn't).

    First question, how does functionurldata(obj) actually run? it seems to just run but other functions that are like test() have to be actually called...do they not? ie. onClick="myurl()"

    Why is var tmp = myurl(); urdata(tmp); after the function? Is that just how scripts work?

    So myurl() spits out a string via return str;, then this is turned into a variable called tmp which in turn is injected into urldata in replace of obj?

    Sorry if I'm asking too many questions...need to understand it so I can make it work. Really appreciate your help/
    Comments are shown in RED
    Code:
    <script type="text/javascript">
    function myurl() {
      var str = 'Data from your function';  // variable created
      return str;                           // returned to calling function
    }
    
    function urldata(obj) {  // variable received as argument of the function
    //blank on purpose
    //this basically prints out data from another script (which resides on myurl)
      alert(obj);            // display variable contents passed to function                        
    } 
    
    // test
      var tmp = myurl();  // function called and returns a string put into tmp
      urldata(tmp);       // function called that prints an alert from the information received
    </script>
    I know nothing about TWITTER as I don't even have an account.
    Any information I could give you on this topic would be worthless.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles