My experience with Javascript is severely limited, which is my disclaimer for the following question/problem.
I have to code links that take the user away from our site with a disclaimer and acknowledgement 'speed bump'. A link clicked on our site that leads to another will generate a pop up window with the verbiage and a button. After they click the button, they are redirected to the intended site. I cannot use server-side scripting with the host we use, but I read that variables can be passed between windows utilizing window.name.
I've decided to use an external file because as I refine this procedure, I need to set cookies that allow the user to choose not to display the message again. But at this point I just want to at least figure out how to accomplish this task before I confuse myself with the cookie issue, since I'm as inexperienced with cookies as I am with Javascript.
The external file includes the following code:
function openExt (id) {
if (window.name==link1) {
id="http://www.consumer.gov/"
}
but I read that variables can be passed between windows utilizing window.name.
I've never heard of that method, and it can't work because as soon as the page changes, all the properties of the window object go bye-bye. If you could do that, you wouldn't have to use the 'name' property of the window object, you could just make one up:
In javascript, you can add a property to any object at will just like that. If you need to, you can send information to another page by appending the information to the url:
htt p://www.consumer.gov/?myinfo=someinformation
On the receiving page, you do this to get the info:
var text = location.search
and in this case that will result in:
text = "?myinfo=someinformation"
Then, you would use a substring function to get everything after the equals sign. However, I don't really see why there is any reason to send any information to the popup. The popup can access any information on the main page using 'opener' to reference the main page.
First, you would never do this:
function openExt (id) {
if (window.name==link1) {
id="htt p://www.consumer.gov/";
because the value you pass to the function is stored in the variable 'id'. If you assign the url to 'id', you overwrite the value in 'id'.
So, let's start with the 1st page:
<a href="htt p://www.consumer.gov/" onclick="return openExt(this)">Consumer Info
from the Federal Government</a>
As far as javascript is concerned, the value of the 'href' attribute is irrelevant because I'm going to prevent the page from going there with some javascript. However, if a user has javascript disabled, and they click on the link, then the only course of action will be to load the href page, so you might as well set the href to the final destination, and then a non javascript user will also be able to get there as well. The 'return' part in red passes the return value of the function openExt() to the onclick event. If you don't have that 'return' word there, it won't matter what the function returns, the onclick event will proceed as normal.
On the page with the above link on it, you are going to link to your external .js file:
Good basic instructions on setting cookies:
http://www.elated.com/tutorials/programming/javascript/cookies/
if you don't know regex's switch to this tutorial for reading cookies:
http://www.quirksmode.org/js/cookies.html
cc.shine
11-30-2004, 09:37 PM
I've been playing around with the code that Warren posted and it does work - Thanks, Warren!! If the final destination link could open up in the pop up window instead of the parent window that would be even better. After spending a while attempting that, I feel I'm shooting in the dark though. However, I'll keep at it, as I'd like to have our site in the background in case the user wants to switch back over to it.
I read some info on the search method, however I believe that is server scripting (??) and our host doesn't allow that us to venture in that direction. Don't ask - it's supposedly a security issue. I did glean some info from this suggestion, especially in regards to supplying an href, just in case.
Boy do I need to learn javascript! Thanks for the direction.
Warren86
12-01-2004, 04:32 AM
Yes. Okay. I'll post the code to make the destination link apprer in the speedbump window, later today. I'll use the method you mentioned, passing the destination link via window.name. I read your initial post several times and I must have misunderstood, because I thought you wanted the destination link to be in the main window.
Warren86
12-01-2004, 05:15 AM
Try it this way. Notice the id string: www_consumer_gov. Periods cannot be used in a window name string. The id is converted to a valid http link in the Speedbump window.
------------- Main document -------------
<HTML>
<Head>
<Script Language=JavaScript>
This is another way to use the window.name method. The window.name is the ordinal number of an array element. The array contains link strings, and is in the main document.
----------- Main Document ----------------
<HTML>
<Head>
<Script Language=JavaScript>
if (popWin != ""){popWin.close()}
popWin = window.open("SpeedBump.html",isLink);
}
</Script>
</Head>
<Body>
<center>
<a href=# onclick="openSpeedBump(0)"> Consumer Info From the Federal Government </a>
</center>
</Body>
</HTML>
------------ SpeedBump.html------------
<HTML>
<Head>
<Script Language=JavaScript>
The first thing you should realize is that there is absolutely no reason to pass any values between your windows.
When you have a main window<-->popup relationship, all the functions and variables on the main page are available to the popup page--so there is no reason to be passing data between pages. The way the popup page refers to those functions and variables on the main page is by using the 'opener' reference. If there is a url on the main page that you want to open in the popup, you can easily access that from the popup. For instance, if a link on your main page is clicked, you can set a global variable(one outside any functions) on the main page equal to the url you want to go to after your 'speed bump' page is displayed:
var url = http://www.consumer.gov;
Then in the popup, you can refer to that url like this:
opener.url
For script inside the popup window, the popup window is refered to using 'window', like this:
window.location.href = ...
For script inside the main window, the popup is refered to like this:
var popWin = window.open("SpeedBump.html","pForm"," toolbar=0,menubar=0, ... );
popWin.location.href = ...
cc.shine
12-03-2004, 03:26 PM
Thanks for all your help, Warren. I used the array code you suggested. I think it will migrate over more easily when I'm ready to code in the cookies.
Thanks again!
Cathy
Warren86
12-03-2004, 03:34 PM
You're welcome. Good luck with your project.
7stud
12-04-2004, 10:22 AM
That's shameful Warren. :(
You led a beginner astray, and then after it was politely pointed out why you were unnecessarily complicating things, you didn't do anything to help her get it correct. That says a lot about your integrity.
cc.shine,
Please consider this code, and notice how much simpler it is than Warren's misguided script. In addition, the following code will also allow users to get to the links should they have javascript disabled, which does include a fairly large percentage of users.
Main page:
<html>
<head><title></title>
<script type="text/javascript" language="javascript">
<!-- Hide from browsers without javascript
var next_page;
function display_message(clicked_link_href)
{
next_page = clicked_link_href;
window.open("message.htm", "", "width=800, height=600, top=100, left=100");
return false; //cancels onclick action so href in link doesn't load in main page
}
// End hiding -->
</script>
</head>
<body>
<a href="http://www.consumer.gov/" onclick="return display_message(this.href)">
Consumer Info From the Federal Government
</a>
</body>
</html>
Speed bump page(message.htm):
<html>
<head><title></title>
<script language="javascript">
<!-- Hide from browsers lacking javascript
function next_page()
{
window.location.href = opener.next_page;
}
// End hiding -->
</script>
</head>
<body>
<div>Warning: Government websites may be hazardous to your health.</div>
If you want to return false from the called function in order to cancel the onclick event, then for the onclick event to receive the return value from the function, you need to include 'return' before the function name.
3) If a user has javascript disabled, the script won't run, so a popup won't open, and the page specified in the 'href' of the clicked link will open in the main page.
cc.shine
12-06-2004, 03:54 PM
Actually what I've learned from this, aside from window.location.href = opener.next_page;, is not to be wary of programmers with "stud" anywhere in their handle.
Thanks stud7 for the lesson and the help. You'll probably be pleased to know that I changed 60+ links from the code Warren suggested to the less confusing and more condensed version you provided. It definitely makes more sense.
I just hope you're cruising the bulletin boards if I ever need help again!:p
MaxBurke
08-10-2008, 04:38 PM
Hi,
I'm having similar problems with a small bit of code I'm putting together, I've been stuck now for a few days, and was reading 7Studs advice but still can't get this code to work, I've got a feeling the Array and its values arent being passed to the new window.
Just if someone witha fresh est of eyes would be kind enough to point out the really easy mistake I've made lol.
<HTML>
<HEAD>
<TITLE>:: Javascript Project::</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGAUGE = " TEXT/JAVASCRIPT">
<!--
var NumItems;
function computercomponents(id, name, spec, desc, funct)
Computer[0] = new computercomponents("CPU","Processor","Pentium 4","Intel Pentium 4 2.60GHZ HT","The Central Proccessing Unit");
Computer[1] = new computercomponents("RAM","Memory","1 Gig"," 1 Gig DDR 333 RAM","Random Access Memory","Tempoary Storage space");
Computer[2] = new computercomponents("MOBO","Motherboard","Pentium 4 Socket 478","Asrock Socket 478","PCB Board that connects all components");
Computer[3] = new computercomponents("VGA","Graphics Card","ATI Radeon 9800 8x AGP"," ATI Radeon 9800 Pro 256MB DDR","Delivers 3D Graphics for gaming");
Computer[4] = new computercomponents("HDD1","Hard Drive 1","120 GB","120 Gigabyte IDE Primary Drive","Stores Operating system, work files and programs");
Computer[5] = new computercomponents("HDD2","Hard Drive 2", "150 GB","150 Gigabyte IDE Secondary Drive","Stores work files and programs");
Computer[6] = new computercomponents("DVD1","DVD+RW Drive","DVD + R RW CDR CDRW Drive","Lite-On DVD+RW IDE Drive","Burns data to DVD +R RW CDR CDRW Discs");
Computer[7] = new computercomponents("DVD2","DVD + -RW Drive"," DVD + - R RW CDR CDRW DL Drive","AOpen DVD + - RW DL IDE Drive","Burns data to DVD + - R RW CDR CDRW Discs");
Computer[8] = new computercomponents("OS", "Operating System","Windows Vista Premium SP1","Microsoft Windows Vista Home Premium SP1","Allows access to files, folders and hardware within a GUI");
NumItems = Computer.length;
function MyNewWindow(copmputercomponents, Computer)
Max, -you really should not 'hijack' a thread with your problem, -even if it IS similar. If the Original Postee marks the thread "closed", many people whom might help you, will consider the thread, well, resolved, and not even read it. I usually do not bother to read threads marked "resolved", and would miss any 'hijacker' posts.
Worse case scenario: thread gets 'locked' and you won't get any replies/follow-ups anyway. :o
Besides, it is best to start your own thread, for archiving purposes. It may turn out that your problem is not similar and anyone later that SEARCHES archives will not find your post/solution.
In short, -don't be afraid to start your own thread. :)
webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved.