Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用脚本标记将HTML注入DOM_Php_Javascript_Jquery_Dom - Fatal编程技术网

Php 使用脚本标记将HTML注入DOM

Php 使用脚本标记将HTML注入DOM,php,javascript,jquery,dom,Php,Javascript,Jquery,Dom,我们允许用户放置 <script language="javascript" src="oursite/script.php"></script> 标签在他们的网页上,然后应该嵌入一些内容从我们的网站到他们的网站。当前script.php包含document.write(“从数据库加载的一些内容”),但是存在一些限制 我是否可以使用jQuery实现同样的功能?我如何告诉jQuery将某段HTML代码放在脚本标记的确切位置?document.write()可以做到这一

我们允许用户放置

<script language="javascript" src="oursite/script.php"></script>

标签在他们的网页上,然后应该嵌入一些内容从我们的网站到他们的网站。当前script.php包含document.write(“从数据库加载的一些内容”),但是存在一些限制


我是否可以使用jQuery实现同样的功能?我如何告诉jQuery将某段HTML代码放在脚本标记的确切位置?document.write()可以做到这一点,但我不确定如何使用jquery做到这一点。(我们已经通过script.php向客户端提供了jquery js代码。

您不需要jquery来执行
document.write()
。只要它是内联执行的(即,不是在
$(document.ready()
)之类的事件处理程序中执行,它就可以工作。只需确保转义end script标记(如下:
),以便HTML解析器不会将其误认为是实际的结束脚本标记:

<script type="text/javascript">
    document.write("<script language=\"javascript\" " +
        "src=\"oursite/script.php\"><\/script>");
</script>

文件。填写(“”);
或者,您可以使用DOM操作添加脚本。如果要在加载页面后添加脚本,这是您唯一的选择。若要将脚本放置在调用它的脚本标记之后,请为脚本标记指定一个id并使用
$(“#myScript”)。在(“”
)之后:


$(函数(){
$(“#myScript”)。在(“”.attr之后({
语言:“javascript”,
src:“oursite/script.php”
});
});

您不一定需要使用jQuery,但可以

这里的基本问题是在加载页面时查找脚本节点

您可以给它一个
id
,希望其他用户的页面上没有重复的id

但是,如果您知道在加载页面时将对脚本进行评估,也可以执行以下操作:

(function(){ // this is just so you don't run into name collisions with the user's page
// Get all script elements
var scripts = document.getElementsByTagName('script');
// we know the last script element will be *this* one
var script = scripts[scripts.length-1];
var newp = document.createElement('p');
newp.textContent = 'inserted paragraph';
// now insert our new nodes after the current script
script.parentNode.insertBefore(newp, script.nextSibling);
})();

当然,您也可以将jQuery与
var$script=$('script:last');
一起使用,但是如果您不需要它做任何其他事情,您可以通过不使用它来节省一些加载时间。

jQuery是一个javascript支持的工具,可以在适当的时候使用。如果您发现“我可以用jQuery实现同样的功能”?可能是
(function(){ // this is just so you don't run into name collisions with the user's page
// Get all script elements
var scripts = document.getElementsByTagName('script');
// we know the last script element will be *this* one
var script = scripts[scripts.length-1];
var newp = document.createElement('p');
newp.textContent = 'inserted paragraph';
// now insert our new nodes after the current script
script.parentNode.insertBefore(newp, script.nextSibling);
})();