SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Springfield, Missouri
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can I make this an external js?

    I am using this script on 51 pages and would like to make it an external .js so I can call it with one line. Below is the code as it is now.

    In Header:
    Code:
    <script type="text/javascript">
    <!--
    
    //Open links in new window option- By spk100 (spk100@yahoo.com)
    //Script featured on Dynamic Drive
    //Visit http://www.dynamicdrive.com for this script and more
    
    function hyperlinks(target)
    {
    if (target) where = "_blank";
    else where = "_self";
    for (var i=0; i<=(document.links.length-1); i++)
    {
    document.links[i].target = where;
    }
    }
    // -->
    </script>
    In Body:
    Code:
    <div><form style="text-align: center" action="get">
              <fieldset style="border: none"><input type="checkbox" name="targetnew"
              onclick="hyperlinks(this.checked)" value="ON" />Check Box to Open Links
              in New Window</fieldset>
            </form></div>
    Also, HTML Kit chokes on this script even though the page it's on validates at W3C. It doesn't like the // --> for some reason.

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First take everything in red
    Code:
    <script type="text/javascript">
    <!--
    //Open links in new window option- By spk100 (spk100@yahoo.com)
    //Script featured on Dynamic Drive
    //Visit http://www.dynamicdrive.com for this script and more
    function hyperlinks(target)
    {
    if (target) where = "_blank";
    else where = "_self";
    for (var i=0; i<=(document.links.length-1); i++)
    {
    document.links[ i]target = where;
    }
    }
    // -->
    </script>
    And paste it into a blank text-file and name it "something.js". I recommend using a subfolder for your js files, just as "js".

    Now, change you script element to follows suit

    <script type="text/javascript" src="js/<EM>something</EM>.js"></script>

    and include that on every page that needs the script.

    That's it!
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Springfield, Missouri
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you but I have done something wrong. I've attached the error message below.

    It's only on this test page right now.

    http://muttcats.com/shelters/alabama3.htm
    Attached Images Attached Images

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just looked at the content of your script and there's an easier way to do that. Just put this in the <head>

    <base target="_self"/>
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Springfield, Missouri
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but the idea of the script is to give the visitor the option. When I had the links open in a new window they complained about it but when I switched them to open in the same window they complained about that too. My average vistor is not very computer savvy and don't know about things like right-click to open in new window so the easier and more obvious I make it the less email I get complaining about things.

  6. #6
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Springfield, Missouri
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a page where the script is working correctly but is not external.

    http://muttcats.com/shelters/florida.htm

  7. #7
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, how could your test page possibly work w/o the checkbox?
    Code:
    <form style="text-align: center">
    	<input type="checkbox" name="targetnew" onclick="hyperlinks(this.checked)" value="ON"> Check Box to Open Links in New Window
    </form>
    Add that in someplace!
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  8. #8
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Springfield, Missouri
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by beetle
    Well, how could your test page possibly work w/o the checkbox?
    Duh! Boy, am I embarrassed. Needles to say, that cleared the problem right up.

    I can't thank you enough for the time you spent helping me out with this.


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
  •