JavaScript仅在jQuery Mobile中刷新页面时起作用 [英] Javascript working only on page refresh in jQuery mobile

查看:120
本文介绍了JavaScript仅在jQuery Mobile中刷新页面时起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将JSP和Struts一起用于jquery mobile.问题是仅在页面刷新一次后才调用javascript函数.该脚本位于数据角色页面"中.但是问题仍然存在.当前正在使用jQuery 1.0稳定版.这是我的代码..

I am currently working with jquery mobile with JSP along with Struts. The problem is the javascript function gets called only after the page is refreshed once. The script is placed within the data-role 'page'. But the problem still persists. Currently am using jQuery 1.0 stable version. Here is my code ..

<body>
    <div data-role="page" id="webtosms">

        <script language="javascript">

        function phonenumlen(){         //Mobile no validation
            var numlen = mobileno.value.length;
            //alert(numlen);
            if(numlen==0){
                alert('Mobile Number cannot be left blank');
                return false;
            }

            else if(numlen<10)
            {
                alert('Mobile number cannot be less than 10 digits');
                return false;
            }
            else
            {
                //alert('true');
                return true;
            }


        }

        function goodchars(e,goods){    // restrict users from entering letters in the mobile number textbox
            var key, keychar;
            key = getkey(e);
            if (key == null) return true;
            // get character
            keychar = String.fromCharCode(key);
            keychar = keychar.toLowerCase();
            goods = goods.toLowerCase();
            // check goodkeys
            if (goods.indexOf(keychar) != -1)
                return true;
            // control keys
            if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
                return true;
            return false;
        }

        function getkey(e)
        {
            if (window.event)
                return window.event.keyCode;
            else if (e)
                return e.which;
            else
                return null;
        }

        langId = 'EN';
        messageLen = 299;
        message = "";

        function checkCount() {
            //alert('function called');

            if(document.webtosms.message.value.length <= messageLen) {
                message = document.webtosms.message.value;
                document.webtosms.charcount.value = (messageLen - document.webtosms.message.value.length);
            }else {
                document.webtosms.message.value = message;
            }
        }

        function getTemplate(){ // code to populate the drop down and display in the textarea


            var where_is_mytool=document.forms[0].cboTemplate.value;
            var mytool_array=where_is_mytool.split("~");

            //alert(where_is_mytool);
              alert(mytool_array);
            window.document.forms[0].smsa.value=mytool_array[0];
            window.document.forms[0].tmplid1.value=mytool_array[1];
            window.document.forms[0].title2.value=mytool_array[1];
            window.document.forms[0].hidlang.value=mytool_array[2];


            window.document.forms[0].hidcreatedbyval.value=mytool_array[5];


        }
  </script>
  </div>

刷新页面后,以上代码绝对可以正常工作.一旦页面已被加载,我不想重新加载该页面.请帮忙.

The above code works absolutely fine once the page is refreshed. I do not want to reload the page once it has already been loaded. Please help.

推荐答案

在包含jQuery之后,但在调用jQuery mobile之前,需要将所有javascript放在头部.

You need to put all of your javascript in the head section AFTER you include jQuery, but BEFORE you call jQuery mobile.

您的头文件应与此类似(第二个文件中为自定义JS):

Your head file should look similar to this (custom JS in second file):

<script src="http://code.jquery.com/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript" src="http://www.example.com/path-to-file/custom-javascript.js"></script>
<script src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>

更新

在页面链接上,添加属性data-ajax="false".如果您想在整个网站范围内禁用Ajax导航,请将以下代码放入自定义JS"文件中:

On the link to the page, add the attribute data-ajax="false". If you'd like to disable Ajax navigation site-wide, put the following code in your Custom JS file:

$(document).bind("mobileinit", function(){
  $.extend(  $.mobile , {
    ajaxEnabled: false
  });
});

以下是文档的链接: http://jquerymobile.com /demos/1.1.1/docs/api/globalconfig.html

这篇关于JavaScript仅在jQuery Mobile中刷新页面时起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆