Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

document.write stylesheet if statement that will always be false

While looking at some page's source, I found this Javascript code just below normal stylesheet definition:

<script type="text/javascript">
    if('' != '')
    {
        document.write("<link type='text/css' rel='stylesheet' href='style.css' />");
    }
</script>

Can someone enlighten me, what is (could be) the purpose of adding something like this to HTML page? Either I am lack of good big coffee this morning, or this part of Javascript is never executed and therefore this particular stylesheet is never added to the document.

So, what is the reason for adding such thing to HTML code? None (a mistake / error) or am I missing something really obvious?

like image 429
trejder Avatar asked Apr 22 '26 06:04

trejder


1 Answers

There is one circumstance that this would make sense, though it is still probably messy - If the server would print a value there so that it it won't be equal, then the stylesheet will be used.

So, on the server in php, it might look like this:

echo "if('{$someVar}' != '')" 

and if $someVar is an empty string, then you get the page source as you currently have it. Perhaps there is some circumstance where it is not empty. If in the code, $someVar could be given a value of (for example) foo, then the page source would say:

if('foo' != '')
    {
        document.write("<link type='text/css' rel='stylesheet' href='style.css' />");
    }

If this were the case, then it would make a lot more sense to go ahead and do the whole process on the server (noted in the comments below by user: still_learning).

if($someVar != '') echo "<link type='text/css' rel='stylesheet' href='style.css' />";

That does give more credit to the possibility that this was a simple on/off switch for a dev to use that wasn't removed. Simply removing the ! would activate the stylesheet again.

like image 77
m59 Avatar answered Apr 24 '26 19:04

m59