简体   繁体   English

如何在特定的JavaScript代码中加载外部HTML页面? 例如,在JS函数中

[英]How to load an external HTML page within a specific JavaScript code? E.g. Within a JS function

I'm new to this website and not sure how it works and whether anybody would reply to my question or not, but worth a try, so will post my question here! 我是这个网站的新手,不确定它的工作原理以及是否有人会回答我的问题,但是值得一试,因此请在此处发布我的问题! :) :)

Basically I've a HTML page which contains some within different parts of the page and here is my code: 基本上,我有一个HTML页面,其中包含页面不同部分的内容,这是我的代码:

<html>
<head>
<title>Welcome to My 1st JavaScript Page</title>
</head>
<body>

<script type="text/javascript">
//
var parameter = document.location.search.replace("?", "").replace("=", "");

// IF NO PARAMETER
if (!document.location.search || !parameter) {
    document.write("No parameter is defined. Please either set ?pictures, ?videos or ?music");

// IF PARAMETER
} else {
    // IF GAMES
    if (parameter == "pictures") {
        // FOR EXAMPLE INCLUDE THE PICTURES.HTML

    // IF VIDEOS
    } else if (parameter == "videos") {
        // FOR EXAMPLE INCLUDE THE VIDEOS.HTML

    // IF MUSIC
    } else if (parameter == "music") {
        // FOR EXAMPLE INCLUDE THE MUSIC.HTML
    }

}
</script>

</body>
</html>

This page would load different things according to the URL parameter that I've set, so when different URL parameters is called, I want to load different external HTML pages within the same page, and don't want to use iframe and such! 此页面将根据我设置的URL参数加载不同的内容,因此,当调用不同的URL参数时,我想在同一页面中加载不同的外部HTML页面,并且不想使用iframe等! Is this possible or not? 这有可能吗? Please have a look at my code above! 请在上面查看我的代码!

In PHP we use: 在PHP中,我们使用:

<?php
    include ("./includes/music.html");
?>

But I don't know how to do this in JavaScript! 但是我不知道如何用JavaScript做到这一点! Could somebody please help me with this! 有人可以帮我这个忙!

Thanks :) 谢谢 :)

You'll need Ajax, something like this: 您将需要Ajax,如下所示:

function include(page) {
    var rq = null;
    if(window.XMLHttpRequest) {
        rq = new XMLHttpRequest();
    } else if(window.ActiveXObject) {
        try { rq = new ActiveXObject("Msxml2.XMLHTTP"); } catch(o) { try { rq = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} }
    }
    if(rq) {
        try {
            rq.open("GET", page, false);
            rq.send(null);
            document.body.innerHTML = rq.responseText;
        } catch(ex) {
            // Provide a fallback here, probably a redirect
        }
    } else {
         // Provide a fallback here, probably a redirect
    }
}

Try using a templating engine to load templates with data. 尝试使用模板引擎将数据加载模板。

There are many templating engines like EJS , mustache , jQuery-tmpl . 有许多模板引擎,例如EJSmustachejQuery-tmpl

The general idea is to request some a template file from the server based on your URL and use the templating engine to render it. 一般的想法是根据您的URL向服务器请求一些模板文件,并使用模板引擎来呈现它。 You can also request JSON data to populate your template with. 您还可以请求JSON数据来填充模板。

If you're okay with learning a new library, the simplest way is to use jQuery and its load method. 如果您可以学习新的库,则最简单的方法是使用jQuery及其加载方法。

There are some other cross-platform ways of using XmlHttpRequest objects to get an HTML page, but jQuery's is by far the simplest. 还有其他一些使用XmlHttpRequest对象获取HTML页面的跨平台方法,但jQuery是迄今为止最简单的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不请求外部页面的情况下重用angular-js通用标记(例如,小部件)? - How to reuse angular-js common mark up (e.g. widget) without requesting external page? 如何在JavaScript的变量名中放置时间戳(例如var a12439853 = 1) - How to put a timestamp within a variable name in javascript (e.g. var a12439853 = 1) 如何在不访问 HTML 的情况下拆分 HTML 页面,例如使用 split.js? - How to split a HTML page without access to the HTML, e.g. using split.js? 如何使用Javascript检查html中的父元素是否是特定的标记名(例如span) - How to check if parent element in html is specific tagname (e.g. span) using Javascript 基于页面滚动在(例如屏幕)内更改内容的响应式粘性框架(例如设备) - Responsive sticky frame (e.g. device) with changing content within (e.g. screens) based on page scroll 如何在popup.html中异步加载外部javascript文件? - How to load an external javascript file asynchronously within popup.html? 是否可以将所有AngularJS文件(例如Controller,Factory)加载到外部HTML中? - Is it possible to load all of AngularJS files (e.g. Controller, Factory) into an external HTML? 将Ember.js与外部代码(例如,Android的WebView)集成的最佳方法 - Best way to integrate Ember.js with external code (e.g., Android's WebView) 如何在特定条件下页面加载时自动从外部js文件执行javascript函数 - How to automatically execute a javascript function from an external js file on page load with a specific condition 如何在Node.js中从客户端调用服务器端功能(例如html button onclick)? - How to call a server side function from client side (e.g. html button onclick) in Node.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM