SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow Linking to External .js file

    Hello All,

    I'm sure the answer to this is quite simple but I am just not getting it.

    I have an existing javascript menu where the user selects a meaning from a drop down box and a list of products with that meaning appear in a search results page.

    I had the whole javascript pasted into one of the columns where I wanted the 'search by meaning' box to appear and it worked fine, however when I tried to validate the HTML (4.01 strict) it popped up errors left and right because it was interpreting the script as HTML.

    I don't know how to write js or work arounds, so I removed the whole script from the page and saved it in a .js file, but I am doing something wrong because I cannot get the javascript to appear at all, nevermind getting it to appear in the column I want it to.

    Below is the javascript itself:

    Code:
    <script>
    <!-- Begin
    
    function selectaplace(form) {
    
    var appname= navigator.appName;
    
    var appversion=parseInt(navigator.appVersion);
    
    if (appname == "Netscape" && appversion >= 3) {
    
    var formindex=form.bymeaningsearch.selectedIndex;
    
    var storage=form.bymeaningsearch.options[formindex].text;
    
    if (form.bymeaningsearch.options[formindex].value != "none") {
    
    var msg=storage+"You are now being transferred to the -> "+storage;
    
    for (var spot=0;spot<msg.length-storage.length;spot++) {
    
    var x=msg.substring(spot,msg.length);
    
    form.bymeaningsearch.options[formindex].text=x;
    
    for(var d=0;d<150;d++) { };
    
    }
    
    window.location=form.bymeaningsearch.options[formindex].value;
    
    form.bymeaningsearch[formindex].text=storage;
    
    } else  {
    
    form.bymeaningsearch[formindex].text="Not a real option!";
    
    for(var d=0;d<1250;d++) { };
    
    form.bymeaningsearch[formindex].text=storage;
    
       }
    
    }
    
    else {
    
    var formindex=form.bymeaningsearch.selectedIndex;
    
    window.location=form.bymeaningsearch.options[formindex].value;
    
       }
    
    }
    
    function makeMyMenu() {
    
    document.write ('<form><select name="bymeaningsearch" onChange="selectaplace(this.form)" size=1>');
    
    document.write ('<option value="">Select Meaning');
    
    document.write ('<option value="/SearchResults.asp?Search=attract">Invoke Attraction');
    
    document.write ('<option value="/SearchResults.asp?Search=beauty">Invoke Beauty');
    
    document.write ('<option value="/SearchResults.asp?Search=confidence">Invoke Confidence')
    
    document.write ('<option value="/SearchResults.asp?Search=creativity">Invoke Creativity');
    
    document.write ('<option value="/SearchResults.asp?Search=fertility">Invoke Fertility');
    
    document.write ('<option value="/SearchResults.asp?Search=friend">Invoke Friendship');
    
    document.write ('<option value="/SearchResults.asp?Search=luck">Invoke Good Fortune');
    
    document.write ('<option value="/SearchResults.asp?Search=wish">Invoke Granting of Wishes');
    
    document.write ('<option value="/SearchResults.asp?Search=happ">Invoke Happiness');
    
    document.write ('<option value="/SearchResults.asp?Search=health">Invoke Health');
    
    document.write ('<option value="/SearchResults.asp?Search=intuit">Invoke Intuition');
    
    document.write ('<option value="/SearchResults.asp?Search=know">Invoke Knowledge');
    
    document.write ('<option value="/SearchResults.asp?Search=lead">Invoke Leadership');
    
    document.write ('<option value="/SearchResults.asp?Search=long">Invoke Long Life');
    
    document.write ('<option value="/SearchResults.asp?Search=love">Invoke Love');
    
    document.write ('<option value="/SearchResults.asp?Search=lust">Invoke Lust');
    
    document.write ('<option value="/SearchResults.asp?Search=magick">Invoke Magickal Ability');
    
    document.write ('<option value="/SearchResults.asp?Search=prosperity">Invoke Prosperity');
    
    document.write ('<option value="/SearchResults.asp?Search=protection">Invoke Protection');
    
    document.write ('<option value="/SearchResults.asp?Search=psychic">Invoke Psychic Ability');
    
    document.write ('<option value="/SearchResults.asp?Search=travel">Invoke Safe Travels');
    
    document.write ('<option value="/SearchResults.asp?Search=strength">Invoke Strength');
    
    document.write ('<option value="/SearchResults.asp?Search=success">Invoke Success');
    
    document.write ('<option value="/SearchResults.asp?Search=obstacles">For Surmounting Obstacles');
    
    document.write ('<option value="/SearchResults.asp?Search=dream">Invoke Sweet Dreams');
    
    document.write ('<option value="/SearchResults.asp?Search=wealth">Invoke Wealth');
    
    document.write ('<option value="/SearchResults.asp?Search=wisdom">Invoke Wisdom');
    document.write ('</select>');
    document.write ('</form>');
    }
    makeMyMenu();
    // End -->
    </SCRIPT>

    Can someone please tell me how I can get the page to link to the .js and place the script in the place where I want it to show?

    Thanks in advance.

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok first of all remove the line makeMyMenu(); at the bottom of your code.

    Then you reference a javascript file using

    Code:
    <script type="text/javascript" src="pathtojsfile.js"></script>
    then the script provided can be called where you want to write the menu.

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
     <HEAD>
      <TITLE> New Document </TITLE>
      <META NAME="Generator" CONTENT="EditPlus">
      <META NAME="Author" CONTENT="">
      <META NAME="Keywords" CONTENT="">
      <META NAME="Description" CONTENT="">
      <script type="text/javascript" src="pathtojsfile.js"></script>
     </HEAD>
    
     <BODY>
      
        <script type="text/javascript">
    
            makeMyMenu();
    
        </script>
    
     </BODY>
    </HTML>
    so simply once you've referenced your js file, place the following code where you'd like the menu to appear:

    Code:
        <script type="text/javascript">
    
            makeMyMenu();
    
        </script>


  3. #3
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have also just noticed that your select options are not correct.

    an Option should be

    Code:
    <option value="somevalue">Some text</option>
    where as your script is writing

    Code:
    <option value="somevalue">Some text


  4. #4
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi and thanks for your help, but even after following your instructions the script does not appear. The firefox error console says:

    Error: makeMyMenu is not defined
    Source File: http://www.site.com/
    Line: 127

    <script type="text/javascript" language="JavaScript">

    makeMyMenu();

    </script>
    and

    Error: XML tag name mismatch (expected SCRIPT)
    Source File: http://www.site.com/meaningsearch.js
    Line: 128, Column: 2
    Source Code: </script>
    Any ideas?

    Also, which is the correct method for the document.write:

    document.write ('<option value="somevalue">Text');</option>
    document.write (<option value="somevalue">Text);</option>
    ...or something else...?

    I didn't write the script or know who did and am unclear if the parenthesis and quotes are necessary or incorrectly placed.
    Thanks.
    Last edited by tde5; Jun 19, 2007 at 13:49. Reason: left out a question

  5. #5
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nevermind I got it...

    I had to remove the

    <SCRIPT LANGUAGE="JavaScript">
    <!-- Begin

    and

    // End -->
    </script>

    from the .js file.


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
  •