43

Is there in HTML (javascript) or other static html tech can:

  • Stop page loading (if browser does not download yet)
  • Stop page rendering (from where the code placed)
  • Stop javascript executed (from where the code placed)

Simply put, is there a code like

<script>window.StopWhateverBelow()</script>

To let browser totally ignore whatever below the code.

UPDATE

I understand the browser may already download the whole thing. What I want is, from the code, page should stopped, for example: if I put the code just after <body> visitor should see blank page, if I put the code in middle of the page, page should be just half like you pressed ESC

ANSWER

As bukko suggested, comments done the trick. But not full, just half If you put <!-- in html page, the rest will be ignored. And in Javascript

document.write('<!--');

Done the trick.

For about make sense:

Here is how this code make sense: when you page load some offpage script, the script is dynamic. When the script code found something wrong (or match some condition), you have one more option to stop the page from rendering, loading, download...

Eric Yin
  • 8,737
  • 19
  • 77
  • 118
  • 1
    It wouldn't make any sense. What do you want to do ? – ldiqual Jan 20 '12 at 14:55
  • are you asking for ability to add comments without them showing up to the average visitor? Makes no sense to stop the browser from interpreting anything... – Jakub Jan 20 '12 at 15:00
  • 3
    @LoïsDiQual This type of negative comments is inappropriate. I just stumbled over a good use case for this (more than 5 year later!) and I'm very happy that this question received good answers, rather than being downvoted for being somewhat unusual. – vog Oct 10 '17 at 08:31

11 Answers11

54

You could do window.stop(); for most browsers besides Internet Explorer. For IE, I had to use document.execCommand('Stop');

cyberboxster
  • 723
  • 1
  • 6
  • 12
21

If you already have html comments on your page, @bukko's solution doesn't fully work. Stuff after the html comment will get rendered normally.

Something else to try is:

document.write('<script type="text/undefined">')

The rest of the document gets interpreted as part of the script tag, and because the script isn't text/javascript, it gets ignored.

Worked for me, thought I'd share it.


Edit

I've seen the script trick not work in Chrome.

This does work, but I have not done extensive browser testing:

document.write('<style type="text/undefined">')
chowey
  • 9,138
  • 6
  • 54
  • 84
  • browser (Chrome) automaticly closes it with `` after any attempt to do this – Owyn Apr 03 '14 at 18:57
  • Same with IE 11. Script tag automatically closed – spicy.dll Aug 14 '19 at 15:05
  • Pretty fun hack, but doesn't seem to work in MS Edge under some cases as well. The effect this has is better in my case than the suggested, `window.stop();` below (depsite being hacky), because with `stop`, elements that have already loaded are suddenly displayed, which can be quite jarring before a redirect. – Lovethenakedgun Jan 31 '20 at 03:52
19
window.stop(); //works in all browsers but IE    
if ($.browser.msie) {document.execCommand("Stop");}; //works in IE, 

document.execCommand works in IE, however it does stop some of FF, NS and some other browsers' functions. Like displaying GIF's animation for example. Using "if browser is IE" makes these two codes work perfectly in all browsers.

Kareem
  • 5,068
  • 44
  • 38
5

Well, there is: <!-- terminated by --> for HTML, but scripts will ignore this.

Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
CompanyDroneFromSector7G
  • 4,291
  • 13
  • 54
  • 97
3

put window.Stop() wherever you want to stop the page from getting renderred

Vicky
  • 45
  • 5
3

What you are asking makes no logical sense. Simply for two reasons:

  • Data is ALREADY sent to the user (HTML / JS) so even tho if you COULD hide content, the data would sitll be there for a user to see (if they view source for instance).
  • Why would you stop 'execution' of a page? It loads simple html structure and reults in a visual display, you should focus on the server site (php for instance) to hide or not send the content in the first place.

If you want to visually hide elements tho, you could use CSS styles (hide divs or the like) with simply adding style="display:none;" to a div like so:

<div style="display:none;">
This text will be downloaded by the user, but hidden from view due to CSS inline style
</div>

If you want to add commenting (thats just for your reference), then use comment formatting:

<!-- this is a comment and will not show up to a user -->

Reference for comments: http://htmlhelp.com/reference/wilbur/misc/comment.html

Jakub
  • 20,418
  • 8
  • 65
  • 92
2

You could also hide the body like that:

var style = document.createElement("style");
style.innerHTML="body { display:none !important; }";
document.getElementsByTagName("HEAD")[0].appendChild(style);
Maciej Krawczyk
  • 14,825
  • 5
  • 55
  • 67
1

Just not much related to the question, but I thought it may be useful for some persons. If you want to jump to other page during page loading use window.location = "somepage.html"; or you can redirect users to the previous page: window.history.go(-1); Useful in JavaScript conditional statements

Bibaswann Bandyopadhyay
  • 3,389
  • 2
  • 31
  • 30
1

If you are using ASP or PHP, HTTP protocol automatically stops but HTTPS protocol don't stop automatically.

To stop it use:

In ASP:

dim r= accept.secure.protocol
r.onload=window.callback('c')

//to firefox,opera,safari

new clientObject(r).access()
// to chrome,ie

forEachElement(a==null);

PHP code:

$a.window ;

All this scripts sends the browserstring "elementcast" by post method

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
1

HTML is static content so the server reads whatever you have written in the file unless you comment it out. For a dynamic file like what you are asking for you need to use php which can do this type of thing.

Iain
  • 11
  • 1
0

The stop methods can break things that have already started to load.

If you want to load everything above a certain point and skip everything below a certain point:

<p>Everything works above this point.</p>
<pre style="display: none !important;">
<p>As long as the PRE tag remains open, 
nothing works below this point</p>
<script>document.write('Nope');
skibulk
  • 3,088
  • 1
  • 34
  • 42