3

Is it possible to prevent the browser from parsing all javascript code present in a file if that browser is (for example, cough cough) ie8? I am curious if this is possible in a theoretical sense, as practically there are workarounds to my particular situation.

Here's what I mean:

(html)

<!--[if lt IE 9]>
    <script type="text/javascript">
        window.browser = 'bad';
    </script>
<![endif]-->

(myfile.js, which with this code, literally won't run in ie8)

if(window.browser != 'bad')
{
    ColorSpace.singletons = {
                get gray() { //get is not correctly parse in <ie8
                    return shadow(this, 'gray', new DeviceGrayCS());
                },
                get rgb() {
                    return shadow(this, 'rgb', new DeviceRgbCS());
                },
                get cmyk() {
                    return shadow(this, 'cmyk', new DeviceCmykCS());
                }
            };
}

Thanks!

(Preemptive edit) For those who might think "wait! javascript is interpreted..." my response is: first the browser does some initial parsing such as splitting it into tokens and the token "get" in this case will break before the code has a chance to be interpreted because it's out of order as far as

matty-d
  • 2,623
  • 3
  • 18
  • 21
  • You just provided the answer to your own question though. Just put that second code-piece inside an ` – Alxandr Aug 01 '14 at 20:22
  • 1
    You are looking for "[Conditional Compilation](http://msdn.microsoft.com/en-us/library/ie/121hztk3%28v=vs.94%29.aspx)" … aso note http://stackoverflow.com/questions/20392163/did-ie11-remove-javascript-conditional-compilation – user123444555621 Aug 01 '14 at 20:23
  • @Alxandr This is within a javascript file that is loaded onto the page. Once the javascript file is loaded it immediately gets parsed. the question is about once it's loaded, if it's possible to somehow prevent that. – matty-d Aug 01 '14 at 20:24
  • @Cheruvian detecting the browser is easy... and not the issue here – matty-d Aug 01 '14 at 20:24
  • 1
    I think you should turn the question upside down though, rather than trying to prevent parsing, load different files (or none at all) for older browsers. Conditional parsing in javascript is (as far as I know) only doable with hacks such as evaling the entire script. – Alxandr Aug 01 '14 at 20:25
  • @Alxandr read my question carefully please. I state that it is work-aroundable.. but that's not the question, Pumbaa80 seems to have the answer – matty-d Aug 01 '14 at 20:26
  • @Pumbaa80 this is the answer I think, make an answer and I'll give you the question! – matty-d Aug 01 '14 at 20:27
  • @blgt read the question please! Again, not a duplicate... – matty-d Aug 01 '14 at 20:28
  • @blgt again... read my question: " I am curious if this is possible in a theoretical sense, as practically there are workarounds to my particular situation." – matty-d Aug 01 '14 at 20:34
  • @blgt, I apologize, I understand now, also a valid answer! Make it an answer and you'll get an upvote – matty-d Aug 01 '14 at 20:37
  • @matty-d You're right, this got a bit too crowded for comments. Posted as an answer, and will delete the previous comments – blgt Aug 01 '14 at 23:54

2 Answers2

0

First step; separate your script into a .js file, say my_awesome_code.js (just noticed you've already done this)

If you can't assume syntax will be accepted by a JavaScript interpreter, the best way to protect yourself is still feature detection but this time with a minimal test case and try..catch

For example, to test let

var can_use_let = false;
try {
    eval('(function () {let i = 0;})');
    can_use_let = true;

} catch (e) {
    if (console && console.warn)
        console.warn(e);
}
console.log("You " + ['can not', 'can'][+can_use_let] + " use __let__!");

At this point you know if you can dynamically include the <script src="my_awesome_code.js"></script> which makes use of these features

Paul S.
  • 64,864
  • 9
  • 122
  • 138
0

As discussed in the comments, you can simply stick the code somewhere where the browser won't treat it as code, and later (ab)use eval() on it. For example, using a string literal:

if(window.browser != 'bad')
{
    eval("ColorSpace.singletons = {\
                get gray() { //get is not correctly parse in <ie8\
                    return shadow(this, 'gray', new DeviceGrayCS());\
                },\
                get rgb() {\
                    return shadow(this, 'rgb', new DeviceRgbCS());\
                },\
                get cmyk() {\
                    return shadow(this, 'cmyk', new DeviceCmykCS());\
                }\
            };");
}

To avoid the awkward backslash-escaping of newlines and quotes, can also use a <script> tag with a type attribute set to a non-javascript (or preferably invalid MIME) type, or indeed any valid/invalid HTML tag which you know the browser won't render (indeed, even

<div style="display:none;">code here</div>`

would do. This code can later be retrieved as a string using .innerText. If you want to source it remotely from another file, you'd have to be a bit more subtle and include it dynamically, as Paul's answer suggests, or have a <script> tag where to change the source attribute, as below

<script id=customScript></script>
// ... and much, much later:
window.browser != 'bad' &&
   document.getElementById('customScript').src="path/to/my/script.js";

Here's a short fiddle showing this: http://jsfiddle.net/4mBBL/


eval has several major drawbacks: debugging it is a nightmare, readability even more so, and overall usefulness of your code drastically decreases with each eval() call, so avoid it whenever possible. Despite this we see it used w̶h̶e̶n̶ ̶w̶r̶i̶t̶i̶n̶g̶ ̶f̶o̶r̶ ̶I̶E̶<̶9̶ all around the interwebs

blgt
  • 8,135
  • 1
  • 25
  • 28