简体   繁体   English

使用javascript包含文件

[英]include file using javascript

I need to include a file always that the screen browser would be greater or equal than 699px, for this I'm trying to use the following script: 我需要始终包含一个文件,以使屏幕浏览器大于或等于699px,为此,我尝试使用以下脚本:

<script type="text/javascript">
<!--
if (screen.width >= 699) {
document.write (" <!--#include virtual="/menu.asp"-->");
}
//-->
</script>

But nothing it does. 但是什么也没做。 Does anyone know how to get it work? 有谁知道如何使它工作?

Best regards. 最好的祝福。

JavaScript fires after the page is passed to the browser. 页面传递到浏览器后,JavaScript会触发。 ASP includes need to be done server side, before page is rendered. ASP包括需要在呈现页面之前在服务器端完成。

The include directive needs to be a part of the page before it gets to the web browser, and since the JavaScript executes in the web browser, it's already too late for that. include指令必须先进入页面的一部分, 然后才能到达Web浏览器,并且由于JavaScript Web浏览器中执行,因此已经为时已晚。 You might consider putting the include in the page, but hiding it in a div with style="display:none;" 您可以考虑将包含内容放入页面中,但将其隐藏在具有style="display:none;"div style="display:none;" , and then removing that using JavaScript when the page loads: ,然后在页面加载时使用JavaScript将其删除:

<script type="text/javascript">
window.addEventListener('load', function(){
    if (screen.width >= 699)
        document.getElementById('myHiddenElement').style.display = null;
});
</script>

Alternatively, you could use the same strategy with CSS Media Queries . 另外,您可以对CSS Media Queries使用相同的策略。

Edit: Example: 编辑:示例:

<style type="text/css">
#myHiddenElement {
    display: none;
}
@media screen and (min-width: 699px) {
    #myHiddenElement {
        display: block;
    }
}
</style>

This will give you better performance, and it will automatically hide and show the content as the user resizes their browser window. 这将为您提供更好的性能,并且当用户调整其浏览器窗口的大小时,它将自动隐藏和显示内容。

<!--#include virtual="/menu.asp"--> is SSI syntax (I think ASP Classic borrows that syntax too). <!--#include virtual="/menu.asp"-->是SSI语法(我认为ASP Classic也借用该语法)。 Either way, if the server supports it, it gets processed on the server and replaced by the content of the file. 无论哪种方式,如果服务器支持,它将在服务器上进行处理并替换为文件的内容。

After all the server side processing is complete, the result is delivered to the client. 在完成所有服务器端处理后,结果将交付给客户端。

The odds are that whatever the content of the file is, it will include " characters and new lines, either of which would break the JavaScript string literal. 赔率是,无论该文件的内容是,它将包括"人物和新的生产线,其中任何一个将打破JavaScript字符串字面。

As a rule of thumb, if you want to show extra content for wider screens when you should: 根据经验,如果要在更大的屏幕上显示更多内容,则应:

  • Always include the content in the page 始终在页面中包含内容
  • Be more concerned about window size than screen size 比屏幕大小更关注窗口大小
  • Use CSS Media Queries to restyle it based on the size 使用CSS Media Queries根据大小重新设置样式
  • Display the content one thing after another on narrower windows instead of hiding some completely 在狭窄的窗口中逐个显示内容,而不是完全隐藏一些内容

If you really want to avoid loading the content at all on narrower screens, then use JavaScript to test for the screen/window size and then use XMLHttpRequest to load additional content and DOM to add it to the page. 如果确实要避免在狭窄的屏幕上完全加载内容,请使用JavaScript测试屏幕/窗口大小,然后使用XMLHttpRequest加载其他内容,然后使用DOM将其添加到页面。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM