博客
关于我
jQuery Mobile页面跳转后未加载外部JS原因分析及解决
阅读量:274 次
发布时间:2019-03-01

本文共 917 字,大约阅读时间需要 3 分钟。

在使用jQuery Mobile开发Web应用时,页面跳转后外部JS未正常运行的问题确实是一个常见的挑战。以下是对该问题的分析及解决方案:

在jQuery Mobile中,页面跳转(pageA => pageB)时,仅将data-role="page"标签元素加载到当前页面中,而不会重新加载之前引入的外部JS文件或page标签外的JS代码。因此,head中引入的外部JS文件及page标签外的JS脚本在跳转后通常无法正常执行,除非页面被刷新。

解决方案

为了确保页面跳转后外部JS文件和page标签外的JS代码能够正常运行,可以采用以下两种方法:

  • 在index页面中注册所有需要使用的外部JS文件,或者使用母版页面来统一管理

    • 这种方法需要在主页面(index)中预先加载所有可能在子页面中使用的外部JS文件。
    • 例如,可以在index页面的head部分引入所有必要的外部JS文件,使其在页面初始化时被加载和解析。
    • 通过这种方式,可以确保即使页面跳转到其他页面,外部JS文件也已被正确加载和解析。
  • 将JS直接写在data-role="page"标签内

    • 这种方法直接将需要在每个页面中使用的JS代码嵌入到对应的data-role="page"标签中。
    • 这样,无论页面如何跳转,JS代码都会被加载和执行,因为它们已经被包含在当前页面的DOM中。
  • 示例

    A网页转跳到B网页,自动弹出正在加载的事件

    在A网页中添加一个超链接指向B网页:

    页眉文本

    页面一

    转到页面二

    页脚文本

    在B网页中添加一个显示加载器的JS脚本:

    页眉文本

    页面二

    页脚文本

    通过这种方法,页面跳转后,B网页中的JS脚本会被正确执行,自动弹出加载中的事件。

    转载地址:http://ydcx.baihongyu.com/

    你可能感兴趣的文章
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>
    NO.23 ZenTaoPHP目录结构
    查看>>
    no1
    查看>>