/    Sign up×
Community /Pin to ProfileBookmark

Replace the content of a page?

How do I overwrite the content of a page with some new stuff?

I throught it was this:

document.write(“<h1>my brand new content</h1>”);

But it seem to add to the content, not replace it…

  • Kasper
  • to post a comment
    JavaScript

    12 Comments(s)

    Copy linkTweet thisAlerts:
    @WebJoelAug 10.2008 — No need to post more than once. As a 'new member', posts are automatically witheld in a 'moderator's queue', for approval. Just a formality really. It's an anti-spam measure that greatly reduces SPAM/junk here. ? Once you attain 'trusted status' (undiscolosed number of posts), you become 'trusted' and your posts automatically appear on-forum when you "submit".

    I don't know the answer to your question, -but someone else will. A Mod. whom 'oversees' this forum is on holiday for awhile... so, patience if no immediate answer. ? I'm sure replies will come.

    Welcome to WD. ?
    Copy linkTweet thisAlerts:
    @GueztAug 10.2008 — What action by the user do you want to trigger the overwrite?
    Copy linkTweet thisAlerts:
    @HoboScriptAug 10.2008 — <i>
    </i>&lt;html&gt;
    &lt;head&gt;
    &lt;script type='text/javascript'&gt;
    function overrideText() {
    document.body.innerHTML = "&lt;h1&gt;This is content overriding.&lt;/h1&gt;";
    }

    <i> </i>window.onload = function() {
    <i> </i> overrideText();
    <i> </i>}
    &lt;/script&gt;
    &lt;/head&gt;

    &lt;body&gt;

    &lt;h1&gt; This is test content &lt;/h1&gt;

    &lt;p&gt;Woooooooo&lt;/p&gt;

    &lt;/body&gt;
    &lt;/html&gt;
    Copy linkTweet thisAlerts:
    @GueztAug 10.2008 — Please post a copy/paste of what that web page shows in your browser.
    Copy linkTweet thisAlerts:
    @Kasper_Hviid2authorAug 10.2008 — 
    <p>Woooooooo</p>
    [/QUOTE]


    Woooooooo indeed! It works perfectly! Thanks for your great help!
    Copy linkTweet thisAlerts:
    @GueztAug 10.2008 — Woops! I got mixed up as to who was posting what. I thought post #4 was posted by the OP, but now I realize it was posted by somebody else. So disregard what I said in my post #5.
    Copy linkTweet thisAlerts:
    @Kasper_Hviid2authorAug 10.2008 — Hey ... is it possible to write the entire page from scratch, from <HTML> to </HTML>?
    Copy linkTweet thisAlerts:
    @GueztAug 10.2008 — Kasper Hviid... I'm not sure exactly what you're asking in post #8. Is it possible to write the entire page from scratch, from <HTML> to </HTML> by doing what exactly?
    Copy linkTweet thisAlerts:
    @Kasper_Hviid2authorAug 10.2008 — Kasper Hviid... I'm not sure exactly what you're asking in post #8. Is it possible to write the entire page from scratch, from <HTML> to </HTML> by doing what exactly?[/QUOTE]

    Sorry, I will try to make a bit more sense!

    post #4 explained how to replace whatever is inside the BODY tag with something else.

    What I want to do, is to replace whatever is inside the HTML tag with something else.

    The reason I want to do this is that there are css stuff inside the HEAD tag which need to be replaced too.
    Copy linkTweet thisAlerts:
    @rnd_meAug 10.2008 — Sorry, I will try to make a bit more sense!

    post #4 explained how to replace whatever is inside the BODY tag with something else.

    What I want to do, is to replace whatever is inside the HTML tag with something else.

    The reason I want to do this is that there are css stuff inside the HEAD tag which need to be replaced too.[/QUOTE]


    a lot of the head items need to be processed whilst the page is loading, or they won't take effect. you can probably use script to apply most of the settings, but it wont be as easy as simply replacing html's html with new html.

    it would likely be more reliable to replace the body content, and then reload external stylesheets/scripts that are in the head.
    Copy linkTweet thisAlerts:
    @Angry_Black_ManAug 10.2008 — What I want to do, is to replace whatever is inside the HTML tag with something else.

    The reason I want to do this is that there are css stuff inside the HEAD tag which need to be replaced too.[/QUOTE]


    it is very possible to look at any tag within an HTML page. simply use the "getElementsByTagName" method. this returns a collection (array) of elements that match your specified tag name. even if there is only one item that matches that name (the "head" tag, for instance), you must still use an index to refer to any specific element in that collection.

    &lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;page title&lt;/title&gt;

    &lt;style type="text/css"&gt;
    body
    {
    background-color: #00ff00;
    }
    &lt;/style&gt;

    &lt;style type="text/css"&gt;
    div
    {
    text-decoration: underline;
    }
    &lt;/style&gt;

    &lt;script type="text/javascript"&gt;
    function info()
    {
    // getElementsByTagName returns an array
    // since there is only one head tag,
    // it would be represented by the first index of the array
    headerElement = document.getElementsByTagName("head")[0]

    <i> </i>children = "the &lt;HEAD&gt; tag containsnthe following tags:nn"

    <i> </i>for(x = 0; x &lt; headerElement.children.length; x++)
    <i> </i> children += headerElement.children[x].nodeName + " (index " + x + ")n"

    <i> </i>alert(children)

    <i> </i>alert("removing the second style tag (index 2) whichncreates the underline within divs")

    <i> </i>headerElement.removeChild(headerElement.children[2])

    <i> </i>children = "the &lt;HEAD&gt; tag now containsnthe following tags:nn"

    <i> </i>for(x = 0; x &lt; headerElement.children.length; x++)
    <i> </i> children += headerElement.children[x].nodeName + "n"

    <i> </i>alert(children)


    }
    &lt;/script&gt;

    &lt;/head&gt;
    &lt;body onload="info()"&gt;
    &lt;div&gt;underlined.&lt;/div&gt;
    &lt;/body&gt;
    &lt;/html&gt;


    unfortunately, according to quirksmode, updating/adding a STYLE tag requires some pretty ghetto workarounds within IE. see the comment section in that quirksmode link for details.
    Copy linkTweet thisAlerts:
    @Kasper_Hviid2authorAug 10.2008 — Thanks! I would never have understood this with any less thorough example. I had no idea that updating the header requiered any of this.

    About adding style tags: I succeeded adding a new css file. It seems to work with both IE and others:

    [code=html]
    <html>
    <head>
    <title>page title</title>

    <script type="text/javascript">
    function info()
    {

    if (document.createStyleSheet) { // IE LIKES THIS ONE
    oStylesheet = document.createStyleSheet( "test2.css");
    }
    else { // OPERA AND FIREFOX LIKES THIS ONE
    var fileref=document.createElement("link");
    fileref.setAttribute("rel", "stylesheet");
    fileref.setAttribute("type", "text/css");
    fileref.setAttribute("href", "test2.css");

    document.getElementsByTagName("head")[0].appendChild(fileref);
    }

    }
    </script>

    </head>
    <body onLoad="info()">
    <div>Hello ...</div>
    </body>
    </html>

    [/code]
    ×

    Success!

    Help @Kasper_Hviid2 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.29,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...