Forum Moderators: open

Message Too Old, No Replies

onMouseOver event and PHP

Can't sort it out!

         

henry0

12:32 pm on Sep 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I know, my JS knowledge is way down the bottom
So don't be surprised by that post :)

I am trying to find a solution for the following problem (PHP and JS)
supplying a link with a target=_BLANK
and setting an event message to let the user know that to come back he/she simply needs closing the new window

So far I have
<<<
echo"<a href=\"/http://www.aaaaaa.com target='_BLANK'\" script type=\"text/javascript\" onmouseover=\"alert('asasas')\">Test Link</a>";
>>>
The event warning shows up fine but when OK is clicked nothing happens, and there is no way to reach the intended URL.
What can I do, or do you have another better suggestion?
thanks.

Fotiman

2:25 pm on Sep 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your generated markup is invalid (missing quotes, extra slashes, and random script).

Try this
echo "<a href='http://www.example.com' target='_BLANK' onmouseover=\"alert('asasas')\">Test Link</a>";

henry0

3:01 pm on Sep 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Fotiman,
Sorry to report that the alert comes OK, clicking on makes it go
but then still nothing happening, it goes nowhere

<edit>
Does it work on your test bed?
</edit>

rocknbil

4:47 pm on Sep 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



echo "<a href='http://www.example.com' target='_BLANK' onmouseover=\"alert('asasas')\">Test Link</a>";

This gives you a "catch-22" - you mouse over the modal alert box comes up. You can't click the link because when you mouse over the alert comes up, unless you leave the mouse over the link and hit enter to respond to the alert. (Which makes the navigation a "puzzle" most users won't be able to solve.) If you try to use the mouse, it will just keep popping the alert and never let you hit the link.


echo '
<!-- line breaks just to prevent fat pages on this site -->
<p><a href="http://www.example.com" target="_BLANK"
onMouseOver="return leaveSite(this.href);">Alert on MouseOver</a></p>
<p><a href="http://www.example.com" target="_BLANK"
onClick="return leaveSite(this.href);">Alert on Click</a></p>
'; <!-- note end of echo here -->

Then include this in an external Javascript file, in the head/foot of the document, whatever, just make it accessible to the current document:


<script type="text/javascript">
function leaveSite(url) {
var conf = confirm('Open a new window and go to ' + url + '?');
if (conf) {
var day = new Date();
var id=day.getTime();
var params = 'width=800,height=600,scrollbars,resizable,toolbar,location';
var win = open(url,id,params);
}
return false;
}
</script>

The return false allows your function to manage the navigation, not the link itself, but if JS is disabled the regular href will open a new window, _blank.

To just pop an alert you can eliminate the confirm and just do alert();


<script type="text/javascript">
function leaveSite(url) {
alert('Opening a new window and going to ' + url + '.');
var day = new Date();
var id=day.getTime();
var params = 'width=800,height=600,scrollbars,resizable,toolbar,location';
var win = open(url,id,params);
return false;
}
</script>

henry0

4:55 pm on Sep 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



rocknbil, thanks
a keeper!
and thanks again for the detailed "how to"

Fotiman

9:34 pm on Sep 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Wow, I totally missed that. That's what I get for a hasty reply. Good catch. :)

henry0

10:28 pm on Sep 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Slightly out of the topic, but not for me!
Do you know of a tutorial or book that deals with JS in conjunction with PHP.
I use AJAX with PHP but by "imitation" not really with "understanding" (understatement)
Thanks all.

rocknbil

3:41 pm on Sep 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What particular angle or approach do you mean?

PHP outputs pages to the browser. Once that page is output, Javascript may or may not be applied. So for Javascript used in your PHP scripts, you just "look at it" as if whatever output is done by your PHP script is what you work with in Javascript.

Sometimes you need to generate Javascript inline as your PHP is output. That is, let's say I generate a product list and want to apply some Javascript to it, for example, to make sure an option is selected before adding it to the cart. So when my script outputs, I generate an array to be used as Javascript. In PHP, this is just a string:


while ($row=mysql_fetch_array($result)) {
$js_list .= "'item\_" . $row[0] . "',";
}

Which leaves a list with a comma at the end, chop it off,

$js_list = preg_replace('/,$/','',$js_list);

Now I can output Javascript directly in my script.

$out .= '
<script type="text/javascript">
var products = new Array(' . $js_list . ');
// Javascript functions here
</script>
';

So whatever you do to your database, whatever pages the user calls, it will output JS based on what you need and without error, without having to edit a JS file every time something changes.

The things to remember about this when outputting Javascript from within a PHP script:

- Escape logical or's that are intended to be used in Javascript, if what you want in JS is ¦¦, use \¦\¦ in your PHP script (**IF** you double quote the output.)

- Escape newlines and other characters normally escaped in PHP, or single quote them so the newline is not interpolated. Since \n is a regular newline in PHP, you need to escape the backslash so you get a newline in JS.

echo "alert('boo\\n')";
echo 'alert(\'boo\n\')';

on output should be
alert('boo');

- form a policy on output delimiters, and stick to it. The previous example also shows I use single quotes for output delimiters, which means I have to escape all single quotes I want actually in the output. IMO singles are better because since they are used a little less than double quotes, you don't get what Larry Wall called the "toothpick syndrome"

echo "<img src=\"some-image.jpg\" width=\"100\" height=\"100\">";

echo '<img src="some-image.jpg" width="100" height="100">';

.. and are allowed to use standard double quotes for HTML attributes instead of single quotes. The down side is doubles don't interpolate, so you have to concatenate more:

echo '<img src="'.$img.'" width="'.$w.'" height="'.$h.'">';

-- When you get in trouble with Javascript mixed with PHP, view the source of your PHP output, save it to your HD. Debug it as a standalone HTML file, then go back and re-apply it to your PHP script, remembering that Javascript is "in the client/browser" and has no real connection to the PHP script.

henry0

4:20 pm on Sep 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A long time ago I went back to college and took a few courses, they tried to force feed me with VB and ASP
and by the same token JS
But I really did not like ASP/Wins and as a direct result did not pay too much attention to JS
Next I very gladly shifted to PHP/Linux.

Now JS is coming back to me with a vengeance!
so I believe I should start learning JS again, pretty much from ground zero.

Thanks for your detailed post, I too often forget about that JS is client side.

rocknbil

8:43 pm on Sep 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The cool thing is, Javascript is **extremely** close to Flash ActionScript, and not too distant from either Java or C/C++. So by learning JS, you're getting a prequel of sorts to these other languages.

VBScript/asp has it's purposes . . . what I like about those is it sorta keeps my Visual Basic programming active, which is used for actual desktop apps. But for the web, yeah, I try to avoid them and talk customers out of Windows servers . . . usually chosen because their home PC is a Windows machine and for that reason, that's what they think they need.