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

javascript - How to replace Current script tag with HTML contents generated by the same script - Stack Overflow

programmeradmin3浏览0评论

I want to replace the current script tag with the HTML contents generated by the same script. That is, my Page is

 <html>
    <body>
       <div>
         <script src="myfile1.js"></script>
       </div>
       <div>
         <script src="myfile1.js"></script>
       </div>
    </body>
 </html>

Inside each .js file corresponding html contents are generated. I want to put the contents as the innerHTML of the parent div. But can't set id for the parent div because the page is not static. So the current script tag must be replaced with the HTML content. How can I do this?

For each script tag src is the same. So can't identify with src. These scripts displays some images with text randomly. Scripts are the same but displays different contents in divs on loading

Please help me

I want to replace the current script tag with the HTML contents generated by the same script. That is, my Page is

 <html>
    <body>
       <div>
         <script src="myfile1.js"></script>
       </div>
       <div>
         <script src="myfile1.js"></script>
       </div>
    </body>
 </html>

Inside each .js file corresponding html contents are generated. I want to put the contents as the innerHTML of the parent div. But can't set id for the parent div because the page is not static. So the current script tag must be replaced with the HTML content. How can I do this?

For each script tag src is the same. So can't identify with src. These scripts displays some images with text randomly. Scripts are the same but displays different contents in divs on loading

Please help me

Share Improve this question edited May 24, 2011 at 11:28 ajay asked May 24, 2011 at 10:37 ajayajay 3553 gold badges8 silver badges18 bronze badges
Add a ment  | 

7 Answers 7

Reset to default 5

try inside of myfile1.js:

var scripts = document.getElementsByTagName( "script" );
for ( var i = 0; i < scripts.length; ++ i )
{
   if ( scripts[i].src == "myfile1.js" )
   {
      scripts[i].parentNode.innerHTML = "new content";
   }
}

This is a great question for those trying to implement a JSONP widget. The objective is to give the user the shortest possible amount of code.

The user prefers:

<script type="text/javscript" src="widget.js"></script>

Over:

<script type="text/javscript" src="widget.js"></script>
<div id="widget"></div>

Here's an example of how to achieve the first snippet:

TOP OF DOCUMENT<br />

<script type="text/javascript" src="https://ajax.googleapis./ajax/libs/jquery/1.6.4/jquery.min.js"></script>
// inside of widget.js
document.write('<div id="widget"></div>');
$(document).ready(function() {
    $.getJSON('http://test.?remote_call=1', function(data) {
        $('#widget').html(data);
    });
});

<br />BOTTOM OF DOCUMENT

Have a look at: http://alexmarandon./articles/web_widget_jquery/ for the correct way to include a library inside of a script.

document.currentScript has been available since 2011 on Firefox and 2013 on Chrome.

document.currentScript documentation at MDN

<!DOCTYPE html>
<meta charset="UTF-8">
<title>currentScript test</title>

<h1>Test Begin</h1>

<script>
  document.currentScript.outerHTML = "blah blah";
</script>

<h1>Test End</h1>

Unfortunately a running JavaScript file is not aware of where it is running. If you use document.write() in the script, the write function will take place wherever the script runs, which would be one way to acplish what you want, but without replacing the contents or being able to perform any actions on the enclosing DIV.

I can't really envisage a situation where you'd have such stringent restrictions on building a page - surely if the page is dynamic you could generate identifiers for your DIV elements, or load content in a more traditional manner?

Why not use Smarty?

http://www.smarty/

You can use javascript in Smarty templates, or just use built-in functions.

Just take a look at http://www.smarty/crash_course

poof -- old answer gone.

Based on your last edit, here's what you want to do:

 <html>
    <head>
        <!-- I remend getting this from Google Ajax Libraries
             You don't need this, but it makes my answer way shorter -->
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
          $(document).ready(function(){
             function getRandomContent(){
              // I expect this is the contents of your current script file.
              // just package it into a function.
              var rnd = Math.random();
              return "[SomeHtml]";
             }
             $('.random').each(idx, el){
                 $(this).html(getRandomHtmlContent());
             });
          });
        </script>
    </head>
    <body>
       <div class="random">
       </div>
       <div class="random">
       </div>
    </body>
 </html>

If you don't mind the script tag remaining in place you can use something as simple as document.write().

myfile1.js:

document.write("<p>some html generated inline by script</p>");

It will do exactly what you need.

发布评论

评论列表(0)

  1. 暂无评论