/    Sign up×
Community /Pin to ProfileBookmark

Comment out code on load

We have a CMS that inputs some javascript into the header, that redirects the page based on a condition. I do not want it to do this, and I would like to comment that out. Rather than going in and editing the php files, which I don’t want to do, I would rather have another line of javascript comment that redirect out.

Is it possible to have a script look for a line in another script, and comment that out?

Thanks!

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Angry_Black_ManApr 04.2008 — it would be possible to rewrite the javascript found on a webpage by:

  • - finding the javascript element on the page

  • - storing the information contained therein into a variable

  • - removing the original element

  • - parsing the variable somehow, wherein you remove the undesired content from the variable

  • - re-append the resulting variable to the webpage.


  • however, lets say your CMS produces the following webpage:

    <script>
    alert("the offending javascript")
    </script>

    ... HTML ...


    since that script is not called by a function, it will fire as the page loads before you have a chance to use other javascript to remove it. if you did manage to place javascript (using your CMS or whatever actually creates the HTML document before it is dished up to the client) into the webpage, you would get erroneous results if the javascript that you want removed is executed as the script is executed during runtime:

    <i>
    </i>&lt;!-- this first script was injected before the HTML document was sent to the client --&gt;
    &lt;script&gt;
    alert("as this page was executed, the following number of javascript were detected: " + document.getElementsByTagName("script").length) // 1... this script
    alert("the detected script contained the follwing information:n" + document.getElementsByTagName("script")[document.getElementsByTagName("script").length - 1].innerHTML) // only this script would appear
    &lt;/script&gt;

    &lt;script&gt;// this was not only [I][B]not[/B][/I] seen by our injected code for removal because it didnt exist as an element on the webpage, but it would also still fire because it isnt called by a function
    alert("psuedo code injected already by the CMS") &lt;/script&gt;

    &lt;!-- or this third script was injected before the HTML document was sent to the client --&gt;
    &lt;script&gt;// we can see all the scripts on this page, but it is too late IF the code we wanted to remove wasnt called by a function and ran outright
    alert("at this point in execution, the following number of javascript were detected: " + document.getElementsByTagName("script").length)
    &lt;/script&gt;


    the ray of light in all of this is that if you can inject javascript into your HTML document BEFORE it is given to the client, and it has a chance to fire before the offending javascript fires, then you can remove the element from the page and replace it with code of your choosing.
    Copy linkTweet thisAlerts:
    @nnhubbardauthorApr 04.2008 — Awesome! Thank you!
    Copy linkTweet thisAlerts:
    @Angry_Black_ManApr 04.2008 — after some testing, this didnt work as expected. i was able to remove the script like i expected, but somehow it still fired:

    &lt;script&gt;
    function go()
    {
    alert("this is the offending code being executed")
    }
    &lt;/script&gt;

    &lt;script&gt;
    alert("Number of scripts in the DOM: " + document.getElementsByTagName("script").length)
    offendingScript = document.getElementsByTagName("script")[0] // hardcoded for example purposes... you'd have to figure out how to pinpoint the offending script where ever it may lie in the DOM
    offendingScript.parentNode.removeChild(offendingScript)
    alert("Removed the first script encountered in the DOM. There are now " + document.getElementsByTagName("script").length + " scripts.")
    alert("The innerHTML for the script still left on the page:n" + document.getElementsByTagName("script")[0].innerHTML)
    &lt;/script&gt;

    &lt;body onload="go()"&gt;
    hello world..
    &lt;/body&gt;


    this is because the script is simply a blueprint for program interpretation. the function "go" was loaded into memory, and the blueprint was no longer being used, but rather now the function loaded into memory. so i tried this:

    [COLOR="DarkGreen"]&lt;script&gt;
    function go()
    {
    alert("this is the offending code that would be executed")
    }
    &lt;/script&gt;

    &lt;script&gt;
    alert("Number of scripts in the DOM: " + document.getElementsByTagName("script").length)
    offendingScript = document.getElementsByTagName("script")[0] // hardcoded for example purposes... you'd have to figure out how to pinpoint the offending script where ever it may lie in the DOM

    <i> </i>parseText = offendingScript.text // warning: "text" property may be IE specific
    <i> </i>alert(parseText)

    <i> </i>/*
    <i> </i>parsing occurs
    <i> </i>parsing occurs
    <i> </i>parsing occurs
    <i> </i>*/

    <i> </i>// hardcoded to simulate parse result from offendingScript
    <i> </i>parseResult = "function go(){alert("new go code")}"
    <i> </i>newScriptText = parseResult

    <i> </i>script = document.createElement("script")
    <i> </i>script.text = newScriptText
    <i> </i>document.getElementsByTagName("head")[0].appendChild(script)

    &lt;/script&gt;

    &lt;body onload="alert('onload will now call go() from within the HTML');go()"&gt;
    hello world..
    &lt;/body&gt;[/COLOR]


    and got expected results. instead of trying to remove the script from the page, we simply redefine it. this is fine because you can still access the original script, grab its information, parse it, remove the bad stuff, and then create a new function with only the good stuff. the onload call will call the same function name, but the new blueprint will have been loaded into memory.
    ×

    Success!

    Help @nnhubbard 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 5.24,
    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: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

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