如何删除已生成的脚本标记并重新生成? [英] How to remove already generated script tag and re-generate?

查看:121
本文介绍了如何删除已生成的脚本标记并重新生成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临一个严峻的问题。我有一个页面有两个部分。在页面的第一部分,html通过ajax请求呈现。在第二页上,html在页面加载时呈现。页面的拳头部分是分开处理的,具有不同的分页脚本。分页也是通过向同一个网址发送ajax请求来处理的。

I am facing a critical problem. I have a page that has two parts. On the first part of the page, the html is rendered through an ajax request. And on the second page, the html is rendered on page load. Fist part of the page is handled separately having different pagination script. Pagination is also handled by sending ajax request to the same url.

我对第一部分有疑问。为第一部分生成html的脚本也使用javascript生成大量的javascript标签和html。现在我在html中插入一个新脚本标签时所做的是这样的:

I have a problem with part one. The script that generates the html for part one is also generating a lot of javascript tags and html using javascript. Now what I am doing when inserting a new script tag in that html is like this:

var script   = document.createElement("script");
script.type  = "text/javascript";
script.text  = "window.twttr = (function (d,s,id) {var t, js, fjs = d.getElementsByTagName(s)[0];if (d.getElementById(id)) return; js=d.createElement(s); js.id=id;js.src=\"//platform.twitter.com/widgets.js\"; fjs.parentNode.insertBefore(js, fjs);return window.twttr || (t = { _e: [], ready: function(f){ t._e.push(f) } });}(document, \"script\", \"twitter-wjs\"));(function(d, s, id) {var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return;js = d.createElement(s); js.id = id; js.src = \"//connect.facebook.net/en_GB/all.js#xfbml=1\"; fjs.parentNode.insertBefore(js, fjs);}(document, \"script\", \"facebook-jssdk\"));var script_generated=1";               // use this for inline script
document.body.appendChild(script);

事实上,Twitter和Facebook连接语句在每个按钮上显示Facebook按钮和Twitter按钮页面第一部分的项目。第一次加载页面时,会成功生成脚本标记。但是当我点击分页时,它会发送一个新的ajax请求以获得新页面的html,它还会再次生成相同的脚本标记。所以在第二页上我的Twitter按钮和Facebook之类的按钮没有显示。

It is in fact Twitter and Facebook connect statements to show a Facebook like button and a Twitter button on each item of the first part of the page. When the page loads for the first time, the script tag is generated successfully. But when I click on pagination, it sends a new ajax request to have html for a new page, it also generates the same script tag again. So on the second page my Twitter button and Facebook like buttons are not being shown.

我虽然是因为Facebook和Twitter的双重连接声明。然后我生成一个变量并在生成该脚本标记时设置一些值。在每个请求中,我检查该变量是否存在。如果它存在,我不生成这样的特定脚本标记:

I though it was due to double connecting statements of Facebook and Twitter. Then I generate a variable and set some value while generating that script tag. And on each request I check whether that variable exists or not. If it exists I don't generate that particular script tag like this:

if(typeof script_generated === "undefined") {
  var script   = document.createElement("script");
  script.type  = "text/javascript";
  script.text  = "window.twttr = (function (d,s,id) {var t, js, fjs = d.getElementsByTagName(s)[0];if (d.getElementById(id)) return; js=d.createElement(s); js.id=id;js.src=\"//platform.twitter.com/widgets.js\"; fjs.parentNode.insertBefore(js, fjs);return window.twttr || (t = { _e: [], ready: function(f){ t._e.push(f) } });}(document, \"script\", \"twitter-wjs\"));(function(d, s, id) {var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return;js = d.createElement(s); js.id = id; js.src = \"//connect.facebook.net/en_GB/all.js#xfbml=1\"; fjs.parentNode.insertBefore(js, fjs);}(document, \"script\", \"facebook-jssdk\"));var script_generated=1";
           // use this for inline script
  document.body.appendChild(script);
} 

这个脚本标签没有重新生成,但是推文和Facebook之类的按钮仍然没有不显示。

Doing this script tag is not regenerated but the tweet and Facebook like buttons still don't show up.

现在我解释了(我可能错了)因为脚本标签是由ajax请求生成的,所以按钮没有正确显示找不到他们的脚本标签。

Now what I have interpreted (I might be wrong) that as the script tags are generated by ajax request the buttons are not properly displayed as they don't find their script tags.

现在我想要的是每当请求被发送到该文件时,它首先检查该特定脚本标签是否存在,如果它存在并生成一个新的,否则生成。请指导我如何做到这一点。

Now what I want is that whenever a request is sent to that file, it first checks whether that particular script tag exists, deletes that if it exists and generate a new one, else generate. Please guide me how to do this.

如果我解释了问题的错误,请告诉我究竟是什么问题以及如何解决这个问题。

If I have interpreted the problem wrong, then please let me know what is the exact problem and how to overcome this.

推荐答案

当你动态添加时,你可以给脚本标记id。

Well you can give id to script tag when you are adding dynamically.

var script   = document.createElement("script");
script.setAttribute('id', 'addedScript');
script.type  = "text/javascript";
script.text  = "window.twttr = (function (d,s,id) {var t, js, fjs = d.getElementsByTagName(s)[0];if (d.getElementById(id)) return; js=d.createElement(s); js.id=id;js.src=\"//platform.twitter.com/widgets.js\"; fjs.parentNode.insertBefore(js, fjs);return window.twttr || (t = { _e: [], ready: function(f){ t._e.push(f) } });}(document, \"script\", \"twitter-wjs\"));(function(d, s, id) {var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return;js = d.createElement(s); js.id = id; js.src = \"//connect.facebook.net/en_GB/all.js#xfbml=1\"; fjs.parentNode.insertBefore(js, fjs);}(document, \"script\", \"facebook-jssdk\"));var script_generated=1";
// use this for inline script
document.body.appendChild(script);

之后,您可以使用此检查脚本标记是否存在,并使用.remove()查询函数删除。

After that you can check if script tag exists by using this and remove using .remove() query function.

if(addedScript != undefined) {
  $("#addedScript").remove();
} 

这篇关于如何删除已生成的脚本标记并重新生成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆