最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can you make a javascript function replace itself on the page with custom html? - Stack Overflow

programmeradmin2浏览0评论

I have some javascript which will create some sort of widget on a page. I will be giving this snippet to clients so I want them to have to do very little.

The most obvious solution which I have work right now looks something like this:

<div id="divContent"></div>
<script type="text/javascript">
    MakeWidget('divContent');
</script>

Make widget basically looks for the divContent div and fill it with my widget's html.

Is there a better way to do this? Can you replace a Script Tag with a Div using Javascript in that Script Tag?

I would really like it if I could reduce the code down to only the MakeWidget function and it would replace itself and the script tag with the html the function generates.

Edit - I essentially want to generate HTML exactly where the MakeWidget function is called on the page.

I have some javascript which will create some sort of widget on a page. I will be giving this snippet to clients so I want them to have to do very little.

The most obvious solution which I have work right now looks something like this:

<div id="divContent"></div>
<script type="text/javascript">
    MakeWidget('divContent');
</script>

Make widget basically looks for the divContent div and fill it with my widget's html.

Is there a better way to do this? Can you replace a Script Tag with a Div using Javascript in that Script Tag?

I would really like it if I could reduce the code down to only the MakeWidget function and it would replace itself and the script tag with the html the function generates.

Edit - I essentially want to generate HTML exactly where the MakeWidget function is called on the page.

Share Improve this question edited Sep 17, 2010 at 19:54 Vyrotek asked Sep 17, 2010 at 19:39 VyrotekVyrotek 5,4395 gold badges49 silver badges73 bronze badges 1
  • @JoshStodola Not as strange as it seems, since the goal eliminates the need for unique ids. – lispmachine Commented Aug 23, 2013 at 10:52
Add a ment  | 

4 Answers 4

Reset to default 6

Can you replace a Script Tag with a Div using Javascript in that Script Tag?

Yes. When the <script> element is reached, assuming it is not a defer or async script, it will be the last script element in the page so far. So you can say, either inline or in an external script:

<script type="text/javascript">
    (function() {
        var scripts= document.getElementsByTagName('script');
        var script= scripts[scripts.length-1];
        var div= document.createElement('div');
        div.innerHTML= 'Whatever content is going to be inserted';
        script.parentNode.insertBefore(div, script);
    })();
</script>

You have to have MakeWidget defined somewhere, right? Presumably this is going to be in an external script. Why not just have the external script source just attach itself to the divContent using the window.onload method?

This would result in this code on your client's page:

<script src="http://foo./makewidget.js"></script>

Your makewidget.js code could then look like this:

window.onload = function() { MakeWidget('divContent') }

There may some issues with other scripts loading and probably some cross-browser patibility issues but that should get you pointed in the right direction.

So you want to have a script element which replaces itself with div.

Your initial code is like this:

<div id="divContent"></div>
<script>
MakeWidget('divContent');
</script>

You can rewrite it like this (I am using JQuery):

<script id="scriptContent">
    $('#scriptContent').after('<div id="divContent"></div>');
    MakeWidget('divContent');
    $('#scriptContent').remove();
</script>

I have not tried it though!

I would think it would be possible to do it as follows:

<div id="divWidgets">
    <script type="text/javascript">
        MakeWidgets("divWidgets");
    </script>
</div>

The end result should be (if I understand your description correctly) that MakeWidgets will replace the contents of the DIV, which, in this case, is the script itself.

发布评论

评论列表(0)

  1. 暂无评论