我目前正在尝试通过onload();
将PHP数组传递给javascript函数在我的SimilarDomains.php中:
<?php
$domainsJS = json_encode($similarDomainsUnique);
?>
<body onload="init(<?php echo "\"$domainsJS\""; ?>);">
我这样做是为了将它作为字符串对象传递,以便稍后使用JSON.parse()处理字符串。在我有的JavaScript中
var obj = JSON.parse(domainsJS);
用于字符串处理。但似乎我有一个SyntaxError:语法错误@第1行。这是HTML Doctype。如果我删除了doctype,它就会转到下一行。它只出现在我身体onload调用php时,就像我一样。
如何处理这个php数组以便在JavaScript中使用。完成所有这些之后,我必须将处理后的值输入到JS数组中。
以下是身体onload在HTML中的含义
<body onload="init("{"0":"estatelawyer.com","1":"reaestatelawyer.com","2":"estately.com","3":"thestate.com","4":"estaterescue.com","5":"boisestate.edu","10":"99acres.com","11":"1point3acres.com","14":"green-acres.com","22":"backcountry.com","24":"baby-kingdom.com","25":"landattorney.com","27":"siteground.com","28":"247realmedia.com","30":"siteground.biz","31":"arealme.com","32":"farming-simulator.com","33":"amkingdom.com","34":"searchengineland.com","35":"shoretelsky.com","36":"grantland.com","38":"amsoil.com","40":"lostrealm.ca","41":"kingdomofloathing.com","42":"shorewest.com","44":"domaintools.com","45":"domain.com.au","46":"realmadridstream.net","47":"farming2015mods.com","48":"travelandleisure.com","49":"landofnod.com","51":"bringmesports.com","52":"cricketcountry.com","53":"bringthebaconhome.com\/user\/dashboard","54":"ollando.com","55":"domain.com","57":"travelandlearntrips.com","58":"scarffruit.country","59":"78land.com","92":"propertylawyer.com","93":"propertylawyergroup.com","94":"propertyattorney.com","95":"rocketlawyer.com"}");">
答案 0 :(得分:0)
你应该只需要echo
直接添加它而不需要任何额外的引号,因为它是一个JSON对象
<body onload="init(<?php echo $domainsJS ?>);">
答案 1 :(得分:0)
将json添加到JS中的var
echo <<<EOT
<script type="text/javascript">//<![CDATA[
var jsn = " . json_encode($similarDomainsUnique);
var obj = JSON.parse(domainsJS);
//]]>
</script>
EOT;
编码很差:
<?php
$domainsJS = json_encode($similarDomainsUnique);
?>
<body onload="init(<?php echo "\"$domainsJS\""; ?>);">
没有理由在PHP模式下来回跳转到HTML模式
每次更改模式都会有PHP开销。
以下是创建HTML页面的基本正确方法。
我喜欢让HTML尽快进入浏览器。这就是为什么我在<body>
标签之后的某处刷新输出缓冲区,并且浏览器将为准备开始渲染做一些事情。
要完成将json传递给<javascript>
我将值赋给$ js然后将$ js嵌入到`
PHP永远不会切换到HTML模式。
<?php ob_start("ob_gzhandler");
header('Content-Type: text/html; charset=utf-8');
header('Connection: Keep-Alive');
header('Keep-Alive: timeout=50, max=100');
header('Cache-Control: max-age=3600');
echo <<<EOT
<!DOCTYPE html>
<html lang="en">
<head><title>Sample</title>
<style>
body{font:400 1emArial,sans-serif;color: #f00 ;}
#page{width:100%;background:#ff0;border:solid .5em #000;padding:2em;}
#contents{max-width:50em;background:#00f;margin:0 auto 0;height:10em;color:#ff0;padding:1em;}
h1{color:#000;text-align:center;}
</style></head><body><div id="page">
EOT;
ob_flush();
$js = "\nvar jsn = '" . json_encode($similarDomainsUnique) . "';\n" ;
echo <<<EOT
<h1>Headline</h1>
<p>Paragraph</p>
</div></div></body>
</html>
<script type="text/javascript">//<![CDATA[
function init(){
$js
var obj = JSON.parse(jsn);
}
window.onload = init;
//]]>
</script>
EOT;
ob_end_flush();
?>
如果您需要obj使用其他功能:
<script type="text/javascript">//<![CDATA[
var obj = '';
function init(){
$js
obj = JSON.parse(jsn);
}
window.onload = init;
//]]>
</script>
EOT;
ob_end_flush();
?>