Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setting variables and calling the script

Status
Not open for further replies.

dthievin

Instructor
Feb 3, 2001
13
CA
First, I’m COMPLETELY a javascript novice, so please endure. Here’s my dilemma:

I’m creating a single javascript called “project.js” in its own directory that I’d like to call from my html for any number of links:

var name="john";
var name="joe";
var name="jack";

function askjohn(){var win=window.open('txt-john.htm','askwinjohn',',scrollbars=no,height=250,width=230');if(!win.opener)win.opener=self}document.write('<a href="javascript:void(0)" onclick="askjohn()">John</a>');

function askjoe(){var win=window.open('txt-joe.htm','askwinjoe',',scrollbars=no,height=250,width=230');if(!win.opener)win.opener=self}document.write('<a href="javascript:void(0)" onclick="askjoe()">Joe</a>');

function askjack(){var win=window.open('txt-jack.htm','askwinjack',',scrollbars=no,height=250,width=230');if(!win.opener)win.opener=self}document.write('<a href="javascript:void(0)" onclick="askjack()">Jack</a>');


I’ve put this line in the header of my html:

<script type="text/javascript" language="JavaScript"src="/scripts/project.js"></script>

Now, I’m sure you javascript whizzes are chuckling at this feeble attempt and for good reason. It doesn’t work because at the top of my rendered page I get a line that looks like this: JohnJoeJack. I have the feeling I could also reduce the three paragraphs in my java to just one, but I haven’t a clue how.

I’d be eternally grateful if someone would point me in the right direction.
 
as dan said, not sure what you are trying to accomplish, but here is a stab at what I think you are going for:

Code:
function ask(person){
var win=window.open('txt-'+person+'.htm', 'askwin', 'scrollbars=no, height=250, width=230');

if(!win.opener) win.opener=self;
}

document.write('<a href="javascript:void(0)" onclick="ask('john')">John</a>');

document.write('<a href="javascript:void(0)" onclick="ask('joe')">Joe</a>');

document.write('<a href="javascript:void(0)" onclick="ask('jack')">Jack</a>');

This will print out the three links (same as before) and when you click on them, each will open a window.

Robert Carpenter
Remember....eternity is much longer than this ~80 years we will spend roaming this earth.
ô¿ô
 
Thanks, Robert:

Yes, the script is meant to open a separate window for any of the html links.

I've tried your version, but without knowledge of the correct syntax in html to call the script at each link. For the "John" link, for example, I tried <a href="javascript:ask(john)">John</a>, but that doesn't work. I appreciate your time.
 
dthevin-

sorry for the delay, but you are missing something very simple in your example. Dont worry though, its a common mistake.

<a href="javascript:ask('john')">John</a>

notice the single quotes around john.

Robert Carpenter
Remember....eternity is much longer than this ~80 years we will spend roaming this earth.
ô¿ô
 
Okay, I give up. So how does one escape the inner quotes?

Thanks

-dt-
 
Actually it doesn't work.

In the head of my html I've set the script and its location:
<script type="text/javascript" language="JavaScript" src="/scripts/project.js"></script>

The JS itself has been modifed as both Robert and Dan suggested:

function ask(person){
var win=window.open('txt-'+person+'.htm', 'askwin', 'scrollbars=no, height=250, width=230');

if(!win.opener) win.opener=self;
}

document.write('<a href="javascript:void(0)" onclick="ask(\'john\')">John</a>');


To call the script in the html, I've added the inner quotes as Robert stated above:

<a href="javascript:ask('john')">John</a>

So I think I've followed your kind advice, but the link still does nothing.

Any further suggestions?
 
Now you've lost me. Why do you have both this line:

Code:
document.write('<a href="javascript:void(0)" onclick="ask(\'john\')">John</a>');

[!]and[/!] this line:

Code:
<a href="javascript:ask('john')">John</a>

When they both do the same thing?

You only need one [!]or[/!] the other, surely?

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Yes, but I also want to call the script from any number of links, whether it's for Jack, John, or Mary. The idea is to get the JS off of my html page since it really just repeats the task and lends unwieldiness.

So I tried keeping the html and eliminating the lines in the script that specify the individuals. Now all that remains of the script is:

function ask(person){
var win=window.open('txt-'+person+'.htm', 'askwin', 'scrollbars=no, height=250, width=230');

if(!win.opener) win.opener=self;
}

The html to call the script for each person is, as you've suggested, <a href="javascript:ask(/'john/')">John</a>

... and still no success. That is why I tried adding the "document.write" lines to the script. A desperate act, for sure.

I really appreciate your efforts.
 
No, I've heard nothing more about this.

I can't articulate it any better than I have above. The task remains unfinished.

Thanks for your help.
-dt-
 
Aaah - if your HTML on the page is this:

Code:
<a href="javascript:ask(/'john/')">John</a>

then you'll need to remove the slashes before the quotes.

You only need to escape them if the links are output with JavaScript.

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Dan:

Thank you, but this doesn't work. Once again, here's the full schmoo:

In the html head:
<script type="text/javascript" language="JavaScript" src="/scripts/project.js"></script>

The script named project.js is in the /scripts folder, and based on the suggestions above, it consists only of:
function ask(person){
var win=window.open('txt-'+person+'.htm', 'askwin', 'scrollbars=no, height=250, width=230');

if(!win.opener) win.opener=self;
}


The html now used to invoke the script is:
<a href="javascript:ask('john')">John</a>

...You'll notice the slashes you first proposed were removed according to your last suggestion. (Which in effect brings us back to Robert's proposed syntax earlier in this post.)

When the link is clicked, it's meant to open another html document named txt-john.htm. Or txt-mary.htm, or anyone I'd like, according to various html links.

For all of the JS gurus, this must be pure baby aspirin. Yet despite its simplicity and the advice posted here, it still doesn't work. Does anyone have any idea why?

Many thanks.
-dt-
 
Ok - start by removing this line:

Code:
if(!win.opener) win.opener=self;

it's just not needed for the job of opening a window.

Then, can you tell us what does happen when you click the link? Do you get any JS errors (are you looking in the JavaScript console in Firefox?)

If nothing happens, try adding an alert into the function, to at least make sure the function is being called.

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I have removed the line and reloaded all files. The result is the same:

In IE there's simply an error message on the bottom toolbar that says "Error on page". In FireFox, with scripts enabled globally, nothing happens whatsoever.

I note that the html does not specify a path. Wouldn't it be necessary? (Although I've tried specifying the path without success.)

-dt-
 
Did you try putting the full url for your popup ?

Code:
<script type="text/javascript">
function ask(person){
var win=window.open("[URL unfurl="true"]http://www.google.com/txt-"+person+".html",[/URL] "askwin","scrollbars=no, height=250, width=250");
}
</script>
<a href="javascript:ask('john')">John</a><br />
<a href="javascript:ask('bill')">Bill</a><br />
<a href="javascript:ask('ben')">Ben</a><br />
<a href="javascript:ask('fred')">Fred</a>
 
I've now tried this with the same non-result. I note that your syntax, Fendal, sets the path (and the other elements) in double quotes, whereas the original script had single quotes. Is that significant?
 
No, it "should" work with either the apostrophe or double quotes. Do you have a pop-up blocker ?, are you running XP service pack 2 ?, if you copy and paste then save and test the following code do you get the alert message ?,

Code:
<script type="text/javascript">
function ask(person){
alert(person);
window.open("[URL unfurl="true"]http://www.google.com/"+person,[/URL] "askwin","scrollbars=no, height=250, width=250");
}
</script>
<a href="javascript:ask('txt-john.html')">John</a><br />
<a href="javascript:ask('txt-bill.html')">Bill</a><br />
<a href="javascript:ask('txt-ben.html')">Ben</a><br />
<a href="javascript:ask('txt-fred.html')">Fred</a>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top