SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Aug 2001
    Location
    DC
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    kickoff a script and call a page in one click?

    I'm trying to simultaneously have a user click a link and have the link open a page as well as kick off a script on my server. How can I do this?

    I'm trying to track whether a certain link is clicked or not. This link takes them to say yahoo.com however I want that same link to kickoff a script as well.

    Any ideas? Help?

    Here is something I tried. I'm using the import script tag to kick off a script however this doesn't work.

    <html>
    <head>
    <title>Untitled</title>

    <script>
    function track()
    {
    alert('create track doc');
    document.write("<scr" + "ipt language='JavaScript' src="someScriptFile" type='text/javascript'></scr"+"ipt>");
    return true;
    }
    </script>
    </head>
    <body>
    <a href="<A href="http://www.yahoo.com">http://www.yahoo.com" onClick="track()">test</a>

    </body>
    </html>
    signature

  2. #2
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    with ie you can use
    Code:
    document.getElementsByTagName('script')[0].src="newJS.js"
    but with the others you will need to put an empty HTML script caller.......
    Code:
    <script type="text/javascript" src="original.js"></script>
    <script type="text/javascript"></script>
    then you can use
    Code:
    document.getElementsByTagName('script')[1].src="new.js"
    hope it helps
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  3. #3
    SitePoint Zealot
    Join Date
    Aug 2001
    Location
    DC
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    going to give it a try

    Cool, I will give it a try and let you know. Thanks.
    signature

  4. #4
    SitePoint Zealot
    Join Date
    Aug 2001
    Location
    DC
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    in IE only

    I cannot get this to work in anything other than IE.

    Maybe I can do this another way. I just need to simultaneously change the location of the page and call a URL on the server (the url is a script and not javascript). I don't care how I do it. What other ways can I call a resource from javascript. I could load an image I guess but the src attribute will by my script and not an actual image.

    any ideas here?
    signature

  5. #5
    Vermicious Knid moncur's Avatar
    Join Date
    May 2003
    Location
    Salt Lake City, UT
    Posts
    274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you tried using your track() function to open a pop-up window that loads the URL from the server?

    Or, what kind of server-side script do you have? It could do whatever it needs to do and then redirect to the destination page, and you wouldn't even need JavaScript.

  6. #6
    SitePoint Zealot
    Join Date
    Aug 2001
    Location
    DC
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by moncur
    Have you tried using your track() function to open a pop-up window that loads the URL from the server?
    Oh, you mean open up a window, have it call the second script, then close the new window, and this while the main page changes to the requested page. Hmmmmm, that might work. I would like to keep things behind the scenes if possible. Popups are so obtrusive but I can give that a try.

    I've also thought about having an intermediary page that can redirect the user to the destination page but I'm worried about the back button on that one.

    The script I'm calling is a Lotus Notes Domino web agent. It is not a javascript file. It creates a tracking document that allows us to track who is actually clicking on the link. I just need to request something from the server to execute the agent, anything really. I just need to have two http requests when the user clicks a link: 1) to call the agent 2) to retrieve and display the page the link is to. That is why Markdidj's idea is appealing to me. I am just having an issue getting it to work in Nutscrape. 97% of our users are IE 5x or above but the person I'm creating this for wants Netscape users as well. I also hate writing IE specific code. I feel like I when if IE and Crapscape both can see it, and I want to win!
    signature

  7. #7
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Buchanp,
    it does work in NS as well, but you cannot put the document.getElementsByTagName in the first js as it hasn't read the 2nd empty script caller.
    try
    Code:
    <html>
    <script type="text/javascript"></script>
    <script type="text/javascript" src="original.js">
    </html>
    then the empty script caller is read first before original.js, and getElementsByTagName('script')[0] can be found in both IE and NS

    Sorry, doesn't seem to work in Opera, just checked
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  8. #8
    SitePoint Zealot
    Join Date
    Aug 2001
    Location
    DC
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Markdidj , I'm actually not doing that. In fact, I have a blank script import with an id assigned to it. Then in the body of the document, I have the function definition for track() which uses the GetElementById to call the function. This function then uses GetElementById to retrieve the blank script import object.

    I use GetElementById instead because I don't want to count on knowing how many script imports there are which I would need to know using getElementsByTagName.

    I will work on it and let you know. I will make a simple example and post it. Thanks for your responses. I appreciate it.
    signature

  9. #9
    SitePoint Zealot
    Join Date
    Aug 2001
    Location
    DC
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is my example that doesn't seem to work in Netscape:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title>Untitled</title>
    <script id="A" type="text/javascript"></script>
    <script>
    function track()
    {
    alert('create track doc');
    var scriptObj = document.getElementById("A" ).src="test.js";
    alert("oneSecond = " + oneSecond);
    return true;
    }
    </script>
    </head>
    <body>
    <a href="<A href="http://www.yahoo.com">http://www.yahoo.com" onClick="track()">test</a>
    
    </body>
    </html>
    The test.js is just a file with . . .
    Code:
    var oneSecond = 1000;
    If the browser is able to successfully import the test.js file, then it would be able to display the alert "oneSecond=1000" else it cannot load the file. IE does it but not Netscape. What am I doing wrong here?
    signature

  10. #10
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You only need one empty srcipt caller. If you were to put it at the start it will always be script[0].
    I haven't tested the getElementById, I only discovered this method the other day.
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  11. #11
    SitePoint Zealot
    Join Date
    Aug 2001
    Location
    DC
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Markdidj -

    Have you tried this from a popup window? I realized that for some reason this script doesn't work from a popup window. Any ideas?
    signature

  12. #12
    SitePoint Zealot
    Join Date
    Aug 2001
    Location
    DC
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I figured out that it definitely had to do with the fact my link was in a popup window. I also had another in an IFrame. Both would not work. I basically had to keep the function definition in the main page that would either popup the window or contain the IFrame. Then when a user clicks on a link inside a document displayed using either the IFrame or new popup window, the clicked link would have to refer back to the original main window and execute that function.

    Here is the code that goes in the parent document that produces the popup window:

    In the head of the parent document, I have the following blank script import:
    Code:
    <script id='alertTrackerScript' type='text/javascript'></script>
    
    // Used to track alerts. This is called by the actual alert document.
    function trackAlert()
    {
    if(document.getElementById && document.all)
    {
    srcLink = "url to my script on the server";
    document.getElementById('alertTrackerScript').src = srcLink;
    }
    return true; 
    }
    
    Then in the popup window document I have the following JavaScript function:
    Code:
    function trackAlert()
    {
      // weeds out non IE browsers b/c it only works in IE
      if(document.getElementById && document.all)
      {
    	window.opener.trackAlert();
      }
      return true; 
    }
    
    function openRegUrl(url)
    {
      window.opener.location = url;
      window.close();
    }
    
    

    Then my link in the popup window is . . .
    Code:
    <a href="javascript<IMG title=embarrasment alt="" src="images/smilies/redface.gif" border=0>penRegUrl('http://www.cnn.com')" onClick="trackAlert()">cnn</a>
    The trick here is that when the user clicks on the link, the trackAlert() function is called which calls the trackAlert() function in the parent document. The parent document's trackAlert() then calls the script on the server. After this is finished, the function returns a true which then causes the browser to open the actual requested url.

    Caveats:

    1) Only works in IE. For me this is an extranet and I know for a fact that 97% of my users are IE 5.x and above.

    2) It looks as if the script call is only called once when initially executed. If the user refreshes the parent page and then calls the script, then it will work again. I believe this is because the browser caches the script import's url after it is assigned. Refreshing the page resets it to unassigned allowing you to reassign it.

    Last edited by Buchanp; Sep 4, 2003 at 13:09.
    signature


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
  •