JavaScript allows you to obtain remote html to share sample code on the current page.

Source: Internet
Author: User
To create a project today, You need to reference a remote html page in the current html page. Baidu once found a very useful code. Here we will share it with you, if you have the same requirements, you can take a look at today's project. You need to reference a remote html page in the current html page. Baidu once found a very useful code, let's share it with you. If you have the same requirements, let's take a look.

Html code

Javascript code

Function clientSideInclude (id, url) {var req = false; // Safari, Firefox, and other non-Microsoft browsers if (window. XMLHttpRequest) {try {req = new XMLHttpRequest ();} catch (e) {req = false ;}} else if (window. activeXObject) {// For Internet Explorer on Windows try {req = new ActiveXObject ("Msxml2.XMLHTTP");} catch (e) {try {req = new ActiveXObject ("Microsoft. XMLHTTP ") ;}catch (e) {req = false ;}} var e Lement = document. getElementById (id); if (! Element) {alert ("The clientSideInclude function cannot find id" + id + ". "+" Your webpage must have a p or span tag containing this id. "); Return;} if (req) {// synchronous request, waiting to receive all content req. open ('get', url, false); req. send (null); if (req. status = 404) {clientSideInclude (id, 'error.html ')} else {element. innerHTML = req. responseText ;}} else {element. innerHTML = "sorry, your browser does not support" + "XMLHTTPRequest objects. This webpage requires "+" Internet Explorer version 5 or later, "+", Firefox or Safari, or other compatible browsers. ";}} ClientSideInclude (includeHtml ," http://XXXXX.html "); // The id of a p in the page is includeHtml

The usage is very simple. The Code has been commented out. Here is a brief description for those who do not read the comments.

The js Code is loaded on the current page or made into a js file. Then, the remote code is automatically written to the p with the idcode of includehtml on the current page.

The preceding section describes how to use JavaScript to obtain remote html code and share the sample code on the current page. For more information, see other related articles in the first PHP community!

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.