简体   繁体   English

如何使用jQuery Mobile重新加载页面更改时的外部JavaScript?

[英]How to reload external javascript on page change using jquery mobile?

I have couple pages within mobile version of the website which use google location services. 我在使用Google定位服务的网站移动版中有几个页面。 I'm using changePage Ajax function to navigate through website pages. 我正在使用changePage Ajax函数浏览网站页面。 On first page load location services works properly , if I go to different page and go back it doesn't. 在第一页上,如果我转到其他页面然后再返回,则定位服务可以正常工作。

When I add rel="external" it works, but the problem is it reloads entire page which is not a big deal for desktop application, but doesn't look nice on mobile. 当我添加rel="external"它可以工作,但是问题是它会重新加载整个页面,这对于桌面应用程序来说不是什么大问题,但在移动设备上看起来并不好。

I tried to add reloadPage = true to changePage but it doesn't make any difference 我试图将reloadPage = true添加到changePage,但没有任何区别

Is there any way to reload just location services without reloading entire page? 有什么方法可以不重新加载整个页面而仅重新加载位置服务吗?

Example: 例:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false" ></script>

$("#btn-loc").click(function () {
        $.mobile.changePage("#btn-loc", { reloadPage: true }, { transition: "slide" });
    });

When you use reloadPage : true you have to reference the location of the external page, not the ID of that pseudo-page element: 当使用reloadPage : true您必须引用外部页面的位置,而不是该伪页面元素的ID:

$("#btn-loc").click(function () {
    $.mobile.changePage("/btn-loc.html", {
        reloadPage : true,
        transition : "slide"
    });
});

Also notice how I passed in the options object, there should just be one with all the properties you want to set in the single object. 另请注意,我是如何传入options对象的,应该只包含一个要在单个对象中设置的所有属性。

reloadPage ( boolean , default: false) reloadPageboolean ,默认:false)

Forces a reload of a page, even if it is already in the DOM of the page container. 强制重新加载页面,即使该页面已经在页面容器的DOM中也是如此。

  • Used only when the 'to' argument of changePage() is a URL. 仅当changePage()的'to'参数为URL时使用。

Source: http://jquerymobile.com/demos/1.1.0-rc.1/docs/api/methods.html 来源: http//jquerymobile.com/demos/1.1.0-rc.1/docs/api/methods.html

Update 更新资料

To add a script to the DOM via JS you can use Google's Analytics code: 要通过JS将脚本添加到DOM,您可以使用Google的Google Analytics(分析)代码:

<script>
    (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = '/path/to/my-file.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();
</script>

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

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