开发者

Javascript does not allow me to set variable with PHP?

I have done this several times before but am wondering if perhaps there is a conflict with my bitly class.

I use php to generate a bitly url from long ones. This is stored in a variable called

$url

I can echo the $url variable and know it works fine. However, when I try and place the into the following javascript function (which is called onclick event), the entire action fails.

function fbs_click() {
    var uf="<?php echo $url; ?>";
    var tf=document.title;
    window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(uf)+'&t='+encodeURIComponent(tf),'sharer','toolbar=0,status=0,width=626,height=436');
    return false;
}

if I replace with an actual URL, i have no problems. Even if I replace with the word "blah", it works. Something about the php echo is throwing it for a loop.

The php echo renders this is source:

var uf=""http://b开发者_开发问答it.ly/rfEcJl\n"";


My guess is that doing this instead will solve your issue:

var uf = <?php echo json_encode($url); ?>;


Is the url is of some file in the file system and wrongly it is giving you '\' instead of '/'? in which case JS might crash... I guess.


It may be that url is not in proper form. So just try to console/alert thr url in "uf" variable, and url in winodw.open(...) statement and then check.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜