SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Guru wii's Avatar
    Join Date
    Sep 2004
    Location
    Denmark
    Posts
    720
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Javascript include and opening new windows ?

    Hi,

    I have a simple javascript from an external provider, something like this:

    Code:
    <script language="javascript"  src="http://external.com/oneinclude.js"></script>
    It works just fine, but I would like all the links that this script includes to open in a new window, is there anyway I can do this editing just the include? As I donīt have access to the file itself.

    Thanks a lot.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Wrap the script in a div with an onclick event that uses window.open for the clicked on link.

    Code html4strict:
    <div id="externalProvider">
    <script language="javascript"  src="http://external.com/oneinclude.js"></script>
    </div>

    Code javascript:
    document.getElementById('externalProvider').onclick = function (evt) {
        evt = evt || window.event;
        targ = evt.target || evt.srcElement;
        // this = the div element
        // targ = the clicked on element
        if (targ.nodeName !== 'A') {
            return;
        }
        window.open(targ.href);
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Guru wii's Avatar
    Join Date
    Sep 2004
    Location
    Denmark
    Posts
    720
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm, where and how do I include the additional javascript code ?

    Thanks

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Place the script at the bottom of the body. You should be placing your scripts just before the </body> tag which improves site performance and makes it easier to target page elements in a quick and easy manner.

    http://developer.yahoo.com/performance/rules.html
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Guru wii's Avatar
    Join Date
    Sep 2004
    Location
    Denmark
    Posts
    720
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is mean is how do I include the additional javascript, I have this before the /head but it doesn&#180;t work:

    Code:
    <script language="javascript" type="text/javascript">
    function externalprovider()
    {
    document.getElementById('externalProvider').onclick = function (evt) {    evt = evt || window.event;    targ = evt.target || evt.srcElement;    // this = the div element    // targ = the clicked on element    if (targ.nodeName !== 'A') {        return;    }    window.open(targ.href);}
    </script>

  6. #6
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One reason it doesn't work is because everything after the first comment marker ("//") on a line is ignored by the script engine.

  7. #7
    SitePoint Guru wii's Avatar
    Join Date
    Sep 2004
    Location
    Denmark
    Posts
    720
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, but I removed the // - still doesn&#180;t work, what am I missing here ?

  8. #8
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Put a set of <script> tags right before the </body> tag and copy and paste the JavaScript code pmw57 posted between them. Make sure there is a DIV with the id of "externalProvider" in the page body.

    If it still doesn't work, post the revised code along with any error message you get. "It still doesn't work" doesn't help us narrow down the problem, especially if you have unknowingly introduced new bugs along the way. In Firefox, you should be using the Firebug extension to debug your code. In IE, click the yellow triangle icon in the status bar, then click Details on the error message.

  9. #9
    SitePoint Guru wii's Avatar
    Join Date
    Sep 2004
    Location
    Denmark
    Posts
    720
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, but the code is not correct, as someone else point out everything after the // is ignored, the error message says syntax error at the last line of the code.

  10. #10
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wii View Post
    Well, but the code is not correct, as someone else point out everything after the // is ignored, the error message says syntax error at the last line of the code.
    That was me who pointed that out. The code pmw57 posted didn't have all the statements on one line like the code you subsequently posted. Did you try copying and pasting it exactly as he wrote it, with one statement per line? If it's all on one line, the syntax error will obviously be on that line, and it will be much harder to determine which statement has the error.

  11. #11
    SitePoint Guru wii's Avatar
    Join Date
    Sep 2004
    Location
    Denmark
    Posts
    720
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I pasted the code in exactly how he wrote it, can you possibly test it yourself, if you have the time ? I have tried other solutions online, but I can&#180;t find a code that fits my needs, and I am in no way a programmer.

    Thanks

  12. #12
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm guessing the external script uses document.write() to print links at the point where you insert the script into your page? Or is it an AdSense or similar script that prints links in another document within an iframe?

  13. #13
    SitePoint Guru wii's Avatar
    Join Date
    Sep 2004
    Location
    Denmark
    Posts
    720
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Exactly, can I PM you the exact javascript include, so you can see it ? Any help is highly appreciated.

    Thanks

    Quote Originally Posted by World Wide Weird View Post
    I'm guessing the external script uses document.write() to print links at the point where you insert the script into your page?


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
  •