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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Call javascript from link 3

Status
Not open for further replies.

Enkrypted

Technical User
Sep 18, 2002
663
US
I'm trying to call a javascript function on a link inside a php page, but appear to have some issues.

I want it to popup a window when someone clicks on a link. I currently have it setup like this:

I have my code as follows:

Code:
<head>
<script type="text/javascript">
function popup(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 
'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=
500,height=500,left = 390,top = 262');");
}
</script>
</head>

<?php
// BEGIN: Added in v2.40.00 - Mantis Issue 0001043
//$index = 0;
//if (!defined('INDEX_FILE')) define('INDEX_FILE', true); // Set to FALSE 
to hide right blocks
//if (defined('INDEX_FILE') AND INDEX_FILE===true) {
// auto set right blocks for pre patch 3.1 compatibility
//	$index = 1;
//}
// END: Added in v2.40.00 - Mantis Issue 0001043

$bgcolor1 = '#171717';
$bgcolor2 = '#171717';
$bgcolor3 = '#171717';
$bgcolor4 = '#171717';
$textcolor1 = '#CCCCCC';
$textcolor2 = '#CCCCCC';

function themeheader() {
	global $module_name, $banners, $admin, $user, $name, $sitename, 
$index, $admin_file, $nukeurl, $slogan, $nukeNAV;
	echo '<body>';
	if (!empty($nukeNAV)) $nukeNAV = '<span class="left"> </span><span 
class="right"> </span>'.$nukeNAV.';
	else {
		$nukeNAV = '<span class="right"> </span><span 
class="left"> </span>
		<a href="./index.php">Home</a>
		<a href="./modules.php?name=News">News</a>
		<a href="javascript:popup('servers.html')">Server Info</a>
		<a href="./modules.php?name=Forums">Forum</a>
		<a href="./modules.php?name=Private_Messages">Private 
Messages</a>
		<a href="./modules.php?name=Your_Account">Account</a>
		<a href="./modules.php?name=Feedback">Contact</a>';
		if (is_admin($admin)) {
			$nukeNAV .= '<a href="./admin.php">Admin</a>';
		} else {
			$nukeNAV .= '<a 
href="./modules.php?name=Search">Search</a>';
		}
	}
	echo '

The Server Info link is what I am trying to get have a popup window appear for, but everytime I put the in the code for the link
Code:
<a href="javascript:popup('servers.html')">Server Info</a>
it just causes the page not to load. Can anyone help me resolve this? TIA!

Enkrypted
A+
 
Your string is surrounded by single quotes, so when php sees single quote before the 'servers.html', it assumes it is the end of the string. You should escape the quotes or use heredoc.
Code:
<a href="javascript:popup(\'servers.html\')">Server Info</a>

Code:
  $nukeNAV = <<<EOD
<span class="right"> </span><span class="left"> </span>
<a href="./index.php">Home</a>
<a href="./modules.php?name=News">News</a>
<a href="javascript:popup('servers.html')">Server Info</a>
<a href="./modules.php?name=Forums">Forum</a>
<a href="./modules.php?name=Private_Messages">Private Messages</a>
<a href="./modules.php?name=Your_Account">Account</a>
<a href="./modules.php?name=Feedback">Contact</a>';
EOD;

[small]Do something about world cancer today: Comprehensive cancer control information at PACT[/small]
 
I've tried inputting as you have it above Vragabond and it still acts like it breaks in that line.

Enkrypted
A+
 
[1]
>if (!empty($nukeNAV)) $nukeNAV = '<span class="left"> </span><span
class="right"> </span>'.$nukeNAV.';


[tt]if (!empty($nukeNAV)) [highlight]{[/highlight]
$nukeNAV = '<span class="left"> </span><span class="right">
</span>'.$nukeNAV; //why .'? you don't have confidence on what you
concat?
[highlight]}[/highlight]
//followed by else { ... line[/tt]

[2] If you want to keep the line without use of heredoc, I think the advice you've received meant this.
[tt]
$nukeNAV = '<span class="right"> </span><span class="left"> </span>
<a href="./index.php">Home</a>
<a href="./modules.php?name=News">News</a>
<a
href="javascript:popup([highlight]\'[/highlight]servers.html[highlight]
\'[/highlight])">Server Info</a>
<a href="./modules.php?name=Forums">Forum</a>
<a href="./modules.php?name=Private_Messages">Private Messages</a>
<a href="./modules.php?name=Your_Account">Account</a>
<a href="./modules.php?name=Feedback">Contact</a>';
[/tt]
[3] Alternative only
>eval("page" + id + " = window.open(URL, '" + id + "',
'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=
500,height=500,left = 390,top = 262');");


Alternatively, you can use this.

[tt]window["page" + id] = window.open(URL, id, 'toolbar=0, scrollbars=0,
location=0, statusbar=0, menubar=0, resizable=0, width=500, height=500,
left=390, top=262');[/tt]

But since the time of gaining popularity of certain frameworks, use of eval() suddenly becomes "cool". Those who had chastised the use of it _out of all proportion_ suddenly themselves squarely shut-up and cook up all the reasons to defense their own use of it. It is up to you.
 
Yeah I tried the 2nd option with having the slash before the quote and it didn't change the result. I'm not really sure how to go about implementing the other 2 options as I am still a novice with PHP

Enkrypted
A+
 
I'm getting an error from your javascript:

Code:
Error: unterminated string literal
Source File: [URL unfurl="true"]http://localhost:8888/jslink.php[/URL]
Line: 5, Column: 50
Source Code:
eval("page" + id + " = window.open(URL, '" + id + "',

Seems you have line breaks in your eval line which should not be there.

Remove all the line brakes so your eval is in a single line.

(Keep the escaped quotes (\') in the PHP section though)

Code:
eval("page" + id + " = window.open(URL, '" + id + "','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=500,left = 390,top = 262');");





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
It appeared it was the eval and an apparent line break. Something so simple and I can't believe I didn't think to check for that. Thanks to all!

Enkrypted
A+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top