SitePoint Sponsor |
|
User Tag List
Results 1 to 12 of 12
-
Aug 12, 2003, 14:14 #1
- 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
-
Aug 12, 2003, 15:27 #2
- 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"
Code:<script type="text/javascript" src="original.js"></script> <script type="text/javascript"></script>
Code:document.getElementsByTagName('script')[1].src="new.js"
LiveScript: Putting the "Live" Back into JavaScript
if live output_as_javascript else output_as_html end if
-
Aug 13, 2003, 07:33 #3
- 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
-
Aug 13, 2003, 08:39 #4
- 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
-
Aug 13, 2003, 14:23 #5
- 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.Michael Moncur - The Quotations Page | My other sites | My Books
JavaScript Weblog | Webmaster Tips
Gadgets | Fitness Weblog | My Weblog
-
Aug 13, 2003, 14:58 #6
- 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.
tryCode:<html> <script type="text/javascript"></script> <script type="text/javascript" src="original.js"> </html>
Sorry, doesn't seem to work in Opera, just checkedLiveScript: Putting the "Live" Back into JavaScript
if live output_as_javascript else output_as_html end if
-
Aug 14, 2003, 08:00 #7
- Join Date
- Aug 2001
- Location
- DC
- Posts
- 113
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by moncur
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
-
Aug 14, 2003, 08:07 #8
- 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
-
Aug 14, 2003, 08:35 #9
- 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>
Code:var oneSecond = 1000;
signature
-
Aug 16, 2003, 15:59 #10
- 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
-
Aug 19, 2003, 11:49 #11
- 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
-
Aug 19, 2003, 12:23 #12
- 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; }
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>
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