简体   繁体   中英

Popup window with javascript and php

i want to open a link via php but with some resources of javascript, otherwise i know that is not possible to do it.

I already have this code, or function :

<script language='JavaScript'>
        function open(URL) {

            var width = 150;
            var height = 250;

            var left = 99;
            var top = 99;

            window.open(URL,'janela', 'width='+width+', height='+height+', top='+top+', left='+left+', scrollbars=yes, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');

        }
    </script>

in php i have this and works fine :

$inf=' | <a href="javascript:abrir(\'http://google.pt\');">+i</a>';

But instead of the static url http://google.pt i want to put a variable of php like this :

$inf=' | <a href="javascript:abrir(\'$VARIABLE');">+i</a>';

of course is not working, i don't know how to embed the variable into that url, any help?

Thanks

use

$inf=' | <a href="javascript:abrir(\''.$VARIABLE.'\');">+i</a>';

or

$inf=" | <a href=\"javascript:abrir('$VARIABLE');\">+i</a>";

The code would be:

$inf=' | <a href="javascript:abrir(\''.$variable.'\');">+i</a>';

If the variable comes from some outside source (like the url, ....) then be sure to make sure that it cannot be used for code injection (thus htmlspecialchars,.... should be used)

If you want to pass a php variable into a JavaScript function then this can be done like this:

$inf=' | <a href="javascript:abrir(\''.$VARIABLE.'\');">+i</a>';

You may notice the single quotes used to mark the php strings and the escaped single quote ( \\' ) used for JavaScript function on mark the JavaScript strings.

Just have found something different that may help =)

I am no way affiliated with pastebin.

Array Implementation

To let the $variable work you have to DOUBLE QUOTE the string, and escape the inner double quotes with "\\" so it doesn't close your string:

$inf=" | <a href=\"javascript:abrir('$VARIABLE');\">+i</a>";

Or you can use concatenation:

$inf=' | <a href="javascript:abrir(\''.$VARIABLE.'\');">+i</a>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM