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!