JavaScript restricted Web pages can only be accessed in the _javascript built-in browser tips

Source: Internet
Author: User

Then the problem comes, this page first involves the mobile Web front-end development, I preferred to use the html5+bootstrap combination to achieve the beautiful effect of the page, the front-end other tasks to the JavaScript solution (here I was completely using native JavaScript code, No frames are used, because the speed at which the phone loads the page is slow, and many of the features that are not used in the framework are loaded with the page, consuming user traffic.

After all the features were in place, the client was very satisfied with the trial and then I submitted the code to the official server. However, the other party suddenly mentioned a point: you make the page is really pretty, compatibility is also good, but this page with the Computer browser is also accessible, then others can be very casual to view the source code of the page, and then copy the entire page, can there be any way to prevent this? Then the problem came again, I was the first time to do micro-letter two development, has not encountered such a demand, how to fix it?

I can not think of it, then I put the problem first put, to do some of the backstage business. In the background of a data acquisition function, PHP code used in the $_server[' http_user_agent ' This parameter, I suddenly think of browser access to the Web page will send a useragent to the server, It contains some basic information about the browser and the user's operating system, since the micro-letter has built-in browser, then use micro-letter browsing the Web page when the useragent will be with the micro-letter related to the unique logo it (after all, Tencent is such a big company, and micro-letter is one of their core products)? Simply print its useragent in code to find out, the JavaScript code is as follows:

Copy Code code as follows:

<script type= "Text/javascript" >
alert (navigator.useragent);
</script>

On my phone, I get the results as shown in the picture:

Clatter really saw a different place, believe that smart friends have found, yes, this is the thing: micromessenger/6.0.0.50_ r844973.501, this string behind the slash is the version number of the micro-letter I'm currently using, which should be a unique identifier for the micro-letter. In fact, I started as a micromessage, thinking of Chinese translation is "micro-news" meaning, but a closer look at the discovery is not, the back of the dictionary to know that the word messenger has "messenger, messenger" meaning, also do not feel strange. The logo should not be available to other browsers, then the solution is here, see the code:

Copy Code code as follows:

<script type= "Text/javascript" >
A regular match to the useragent of the browser, other browsers that do not contain the unique identity of the micro-letter
var useragent = navigator.useragent;
if (Useragent.match (/micromessenger/i)!= ' Micromessenger ') {
This warning box will block the current page from continuing loading
Alert (' This visit has been disabled: You must access this page using a micro-mail built-in browser! ');
The following code is to forcibly close the current page with JavaScript
var opened = window.open (' About:blank ', ' _self ');
Opened.opener = null;
Opened.close ();
}
</script>

This code is tested on Android, IPhone, IPad, PC, as long as it is not open in the micro-letter inside the page, first will pop up the warning box, at this time the page behind is blank, nothing has been loaded, when the click of the warning box to confirm the button, The last three lines of code will force the current page to close. OK, to here is realized the user originally meaning, can simply wrap up.

Wait, is it really okay to wrap up like this? Are you sure there's no other problem? Of course not, in fact this method does not let you rest easy, because for the forgery of the useragent, or can bypass this limit, in general is to prevent a gentleman from the villain, we understand. Of course, if a friend has a better way to solve this problem, but also trouble in the following comments to inform, technology lies in mutual sharing and exchanges, hehe.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.