-1

I have a website with some code like

<html>
    <head>
         ...
    </head>
    <body>
         ...
    </body>
</html>
<script src="./file.js"></script>

But when I open in browser, it automatically corrects by moving the <script> tag above <body> tag, as follows and includes the file:

<html>
    <head>
         ...
    </head>
    <body>
         ...
         <script src="./file.js"></script>
    </body>
</html>

I know that its due to HTML Validations, but Is there any way to prevent such "Auto-Correction"?

I don't need that file.js to be executed(the file has been added by my hosting provider automatically for Ad purposes)

The problem is that the page redirects on mobile access

3
  • Buy a hosting. 2$ / month. You can view my profile ;) Nov 18, 2015 at 11:08
  • 1
    If the script tag is just appended you can try to add an opening comment or a <noscript> tag. But the hoster won't be happy. Nov 18, 2015 at 11:13
  • I can't believe that worked... Thanks @RolandStarke Nov 18, 2015 at 11:35

3 Answers 3

1

(the file has been added by my hosting provider automatically for Ad purposes)

Then it's them who are doing that and not the browser. Before your page is sent out to browser they add that to it. Browser doesn't care about where your elements are located and where they should be.

For any practical purpose however even if that script file is included at the end of the file it will still execute.

2
  • Yes, but is there any way to prevent script outside html tag from being executed? Probably by using a script that would strip last few lines? Nov 18, 2015 at 11:07
  • Nope there is not, they have the control over your page so anything you do will be before it gets to them and they can still add it so no point in wasting time over it. Get some other hosting provider Nov 18, 2015 at 11:08
0

The script will be run regardless. If you know what the script execution does there might be ways to prevent it from actually doing anything, but it will still execute no matter its placement on the page.

navigator.__defineGetter__('userAgent', function(){
    return 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36';
});

Answer to the question: how to manually set user Agent so as make js unable to detect mobile access

2
  • Their script uses document.location redirect to their home page when viewed from mobile. I've tried document.onbeforeunload but still no luck.. Nov 18, 2015 at 11:26
  • navigator.__defineGetter__('userAgent', function(){ return 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36' }); try this at the top of your page
    – Elentriel
    Nov 18, 2015 at 11:40
0

Option 1:(by @RolandStarke)

Add a <noscript> after the html tag.

Option 2:(by @Elentriel)

Force change User-Agent by appending the following script to the top of the file.

navigator.defineGetter('userAgent', function(){ return 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36' });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.