Jump to content

<!-- //--> What does it mean?


terryds
 Share

Recommended Posts

I see there is a javascript statement like this :

[color=#000000]<!--[/color][color=#000000]if (screen.width <= 700) {[/color][color=#000000]window.location = "http://m.yourdomain.com";[/color][color=#000000]}[/color][color=#000000]//-->[/color] [color=#000000]

[/color] Can you tell me what does the <!-- //--> mean ? Is it a comment? But.. if it's a comment, so the statement inside won't be executed, right? I know that code from http://www.webhostinghub.com/support/website/how-to/mobile-redirect

Edited by terryds
Link to comment
Share on other sites

Javascript/ECMAScript has two types of comments, single line ones starting with

// this is a comment

and multiple line ones

/*This is  a commenton several lines*/

The code

<!-- -->

is not a Javascript comment, however Javascript code is often embedded inline in HTML or XHTML documents where

<!-- -->

are comment delimiters.As for code inside such comments not being executed, well with

<!--<script>window.location = 'foo.html';</script>-->

the whole script element is commented out in the surrounding HTML and that way the script code is ignored and not executed.On the other hand you might find

<script><!--window.location = 'foo.html';//--></script>

That approach dates back from the ancient past in terms of web development when there were browser not knowing and supporting the "script" element and others supporting it; to ensure that the browsers not supporting the "script" element did not render the script code as text on the page you would comment out the contents of the "script" element and the browsers supporting the "script" element would execute the embedded code although it was commented out. This approach is however no longer needed and not recommended in my view.

Link to comment
Share on other sites

Like Martin said, that's to help with any browser that doesn't support the script element. If you can name a browser that is in common use and doesn't support the script element, then you know more than I do (I'm not talking about a browser that doesn't support Javascript, but one that does not recognize the script element at all and will render the code as text on the page). I don't even think I've ever used a browser in my life that hasn't supported the script element. Apart from that, about the code that you're linking to. They're trying to tell you how to detect a mobile browser, and they say this:

One possible way to detect mobile browsers is by using Javascript to detect the screen width. The idea is that mobile devices will not have a screen width wider than a certain pixel size (e.g. 700 or 800). Users with a screen width less than what you specify will be redirected to the mobile site.
Then they say this:
While this is probably the easiest method to implement, it does have a drawback. Not every mobile browser fully supports Javascript (or the user can disable it) and the redirection will fail.
I just want to point out that the drawback is not that Javascript might not be supported, the drawback is that using the width to determine what they are using is a stupid idea. My cell phone has a resolution of 1920 x 1080, which is a common desktop (or laptop) resolution. Checking the width doesn't tell you what they are using. A much better, and still simple, way to check is described here: http://www.abeautifulsite.net/blog/2011/11/detecting-mobile-devices-with-javascript/ A much more accurate and detailed API is available here: http://blog.mobileesp.com/
  • Like 1
Link to comment
Share on other sites

There are some examples here: http://blog.mobileesp.com/?page_id=101 As that page notes, doing detection with PHP is much more reliable than doing it with Javascript, like the site you originally linked to said there are several mobile devices that don't support Javascript. Redirection in Javascript always happens the same way, but setting window.location.href to the URL you want to redirect to. Look through the list of Javascript functions in their API to see what kinds of things it will detect. The same functions are available in the PHP API, which will be more reliable because it doesn't matter what the phone supports.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...