views:

75

answers:

5

I am looking for such a method which allows me to add, remove DOM objects of a page before the DOM has even rendered, I mean the first JS that i include in the head tag of my page can come up, look for a specific tag say and remove it from the HTML even before it is rendered, I know that the javascript call is blocking so as the browser will be rendering my page and will come to my javascript in head it will block the rendering and download and excecute the javascript, now at the moment of execution i need control of the previously downloaded HTML waiting to be rendered. Is there any such way?

I have tried using document properties but the i dont get the tags in my js which come after this js in the html.

A: 

Insert the JavaScript just before </body>.

mcandre
thank you mcandre, but this is the issue i want to remove those tags before being rendered and for that i need my removal js to run before anything.
Umair
A: 

You've got to be careful with this, as trying to modify the DOM before it has been completed causes problems in IE. Particularly in IE6 this would cause a very unfriendly error message, which after accepting causes the standard "This Page cannot be displayed" page. Although not completely recitified in IE8, this problem is simply swallowed.

If its possible, I would recommend stripping the required tags out of the html before it gets to the renderer, either through a proxy, or a browser extension. If these are not really feasable, then I'm not sure what you can do.

Matthew Abbott
that means there is no such way of doing it with Javascript ? no crossbrowser solution? :(
Umair
Not really, you can't access and modify the DOM if it hasn't been constructed. The DOMContentLoaded is probably the event you want to watch, but I'm not sure which browsers it is currently supported by.
Matthew Abbott
I tried that, no luck it also doesnt fire up before the rendering
Umair
A: 

Perhaps another way of attacking the problem is from the other direction. Start with everything hidden, then only show the elements you want once the page is loaded. Not sure if your problem domain allows for that, but it might work for you.

Vinny Brown
yes it would have been a solution, but what i want to remove is another javascript, i dont wnat it to be downloaded and this seemed to be possible if i could just removed that dom tag before the rendering of that tag, in this way the browser will block rendering, read my removal js and remove that script tag before its download. but the chances are quite low now
Umair
You would not want to use this method either. If you are trying to do progressive enhancement and you hide everything with CSS your page will look good on those browers that have ecmascript, but not the ones that don't (it will still be hidden).
John
A: 

So, you want to prevent a script in the page from executing? If you can put your own script where-ever you want, you could try

<script>
document.write("<!--");
</script>
<script>// offending script</script>
<script>
document.write("-->");
</script>

(Warning: that way lies madness.)

Ms2ger
well what i want is completely stopping that script from getting downloaded
Umair
A: 

A short and simple answer to this question is NO, this cant be done

Umair