博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jQuery Mobile 脚本加载问题
阅读量:4313 次
发布时间:2019-06-06

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

刚开始使用jQuery Mobile,发现很多问题需要重新考虑,比如脚本加载问题。

在普通html中,如果a.html中有链接到b.html,b.html中有类似代码:

$(document).ready(function() {

alert("hello");
});

则这段代码可以被正常执行。

 

而在jQuery Mobile中,这样做就行不通了,在浏览器中直接刷新b.html,则代码可以正常执行,而从a.html跳转到b.html时则不会被执行!为什么?

 

参见:

http://www.cnblogs.com/pinocchioatbeijing/archive/2013/12/08/3463857.html

第一次做用jQuery Mobile做东西,发现一些跟平时的思维习惯不太一样的。其中这个框架的页面加载机制便是其中一个。如果不明白其中的奥秘,往往会出现一些让人摸不着头脑的怪现象,比如页面进入后点击按钮后Javascript就是不执行,而用F5刷新页面后又可以正常执行等。

即使我们明白了HTML文件与jQuery Mobile中page概念的区别,也还是不能解决上述问题。当然,了解这个是一个大前提。原来,jQuery Mobile是用Ajax的方式加载所有HTML中的标记data-role="page"的DIV元素中,第一个HTML页面一般都是完全加载,包括 HEAD BODY 都会被加载到DOM中,完成后便是链接到的其他页面内容的加载。 第二个HTML页面只有 BODY 中的内容会被以Ajax的方式加载到头一个HTML的 DOM中。 并且第二HTML页面的 BODY 中的内容也并非全部加载,而仅仅是其中的第一个带data-role="page"属性的DIV会被加载进去,其余的东西则无缘进入页面渲染。

直接上代码,或许更容易让人明白些:

index.html

                        
TEst

 

test.html

            
Foobar

Bar

 

好了,再看这篇文章:

http://stackoverflow.com/questions/17403825/link-fails-to-work-unless-refreshing/17403907

他还有另外一篇文章:

http://www.gajotres.net/how-jquery-mobile-page-handling-affects-javascript-executions/

 

How jQuery Mobile page handling affects javascript execution

For us to understand this situation we need to understand how jQuery Mobile works. It uses Ajax for page loading into the DOM.

 

First page is loaded normally. Its HEAD and BODY is loaded into the DOM. That content will stay there (unless page is refreshed) to await further content loading. When second page is loaded, only its BODY content is loaded into the DOM, and when I say its BODY content I mean DIV with an attribute data-role=”page” and its inner content.

 

This may not sound like something problematic, but you should think twice. What if we have several HTML pages and every and each page has something unique, let’s say different javascript intended to be used only during that page execution, not to mention additional CSS files. Everything found in a HEAD of those files are going to be discarded, and its javascript is not going to be executed.

 

Unfortunately, you are not going to find this described in their documentation. This is either thought to be a common knowledge or they just forgot to mention it.

 

There are several solutions to this problem; some are good and some are bad, everything should depend on a project architecture.

 

Intro

 

This article is an response to my Stackoveflow answer that can be found .

 

Solution 1

 

In your second page and every other page, move your SCRIPT tag into the BODY content, like this:

1
2
3
4
5
6
7
8
<
body
>
    
<
div
data-role
=
"page"
>
        
<
script
>
            
// Your javascript will go here
        
</
script
>
        
// And rest of your HTML content
    
<
div
>
</
body
>
 

This is a quick solution but an ugly one.

 

Working example can be found in my other answer here: 

 

Another working example: 

 

Solution 2

 

Move all of your javascript into the original first HTML. Collect everything and put it into a single js file, into a HEAD. Initialize it after jQuery Mobile has been loaded.

 
1
2
3
4
5
6
<
head
>
    
<
meta
name
=
"viewport"
content
=
"width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"
/>
    
<
link
rel
=
"stylesheet"
href
=
""
/>
    
<
script
src
=
""
></
script
>   
    
<
script
src
=
"index.js"
></
script
> // Put your code into a new file
</
head
>
 

At the end of this article, I will describe why this is a good solution.

 

Solution 3

 

Use rel=”external” in your buttons and every elements you are using to change page. Because of it, Ajax is not going to be used for page loading and your jQuery Mobile app will behave like a regular web application. Unfortunately, this is not that good solution. If Ajax is not used for page loading, you will loose a lot of functionalities that make jQuery Mobile such a great framework.

 
1
<
a
href
=
"#second"
class
=
"ui-btn-right"
rel
=
"external"
>Next</
a
>
 

Official documentation, look for a chapter: .

 

Realistic solution

 

Realistic solution would use solution 2. But unlike solution 2, I would use that same index.js file and initialize it inside a HEAD of every possible other page.

 

Now, you may ask WHY is that?

 

jQuery Mobile is buggy, and sooner or later there’s going to be an error and your app will fail (including loaded DOM) if your js content is inside a single HTML file. DOM could be erased, and browser or you will refresh your current page. If that current HTML page don’t have javascript initialized inside its HEAD then that web app will not work until everything is restarted.

 

In the end, when creating a jQuery Mobile application spend some time thinking about a page architecture. If you need a help take a look at my other where I am discussing secrets of a good jQuery Mobile architecture.

 

好吧,最后总结一下:

 

如果多个html,请将第二个页面的脚本放在第一个page后面,紧跟在page后面,像这样:

  
.......

 

转载于:https://www.cnblogs.com/GarfieldTom/p/4555489.html

你可能感兴趣的文章
Navicat远程连接云主机数据库
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
【2020-3-21】Mac安装Homebrew慢,解决办法
查看>>
influxdb 命令行输出时间为 yyyy-MM-dd HH:mm:ss(年月日时分秒)的方法
查看>>
FFmpeg 新旧版本编码 API 的区别
查看>>
RecyclerView 源码深入解析——绘制流程、缓存机制、动画等
查看>>
Android 面试题整理总结(一)Java 基础
查看>>
Android 面试题整理总结(二)Java 集合
查看>>
学习笔记_vnpy实战培训day02
查看>>
VNPY- VnTrader基本使用
查看>>
【NOI 2018】归程(Kruskal重构树)
查看>>
注册用户
查看>>
HDU 4571 SPFA+DP
查看>>
centos 创建以日期为名的文件夹
查看>>
Java Timer触发定时器
查看>>
Page Object设计模式
查看>>
程序的基础知识
查看>>
在VIM中使用GDB调试 – 使用vimgdb
查看>>
python爬虫---从零开始(五)pyQuery库
查看>>
Centos MySQL数据库迁移详细步骤
查看>>