用户
 找回密码
 入住 CI 中国社区
搜索
查看: 4179|回复: 15
收起左侧

[视图] javascript 不执行,刷新后才执行

[复制链接]
发表于 2017-9-12 16:06:03 | 显示全部楼层 |阅读模式
页面中 有Javascrip, body onload 方法 不执行,刷新页面后才执行,其他 页面的javascript也存在这个问题,菜鸟求助。
    <script>
        function isLoad(){
            //localStorage.clear();
            if(localStorage.userid){
                //$("#userid").val(localStorage.userid);
                $("#userid").text( "u"+localStorage.userid);
                $("#prelogin").css('display','none');
            }
            else $("#user_div").css('display','none');
        }

    </script>
</head>
<body onload="isLoad()">
如果单独打开页面是可以执行的,在controller中使用$this.load.view就不执行
 楼主| 发表于 2017-9-18 15:53:18 | 显示全部楼层
Hex 发表于 2017-9-15 11:43
登录成功后,你是用的什么方式跳转的?贴你的跳转代码看一下。


$('#login').on('click', function () {
            // get the value from the username field
            var phone = $('#phone').val();
            var code = $('#code').val();
            $.ajax({
                type: 'POST',
                url: 'http://' + ip + '/webticket/index.php/ajax/user_verify',//路径
                data: {'phone': phone, 'code': code},
                dataType: 'json',
                success: function (msg) {
                    if (msg.flag == 1) {
                        window.localStorage.setItem("userid",$('#phone').val());
                        //localStorage.userid = ;

                        alert("logged in");

                        location.href= "http://"+ip+"/webticket/index.php/cuser" ;
                    } else {
                        alert("log failed");
                        //location.href= "http://"+ip+"/webticket/index.php/cuser" ;
                    }
                }
            })

        });
 楼主| 发表于 2018-1-21 08:57:19 | 显示全部楼层
Linking within a multi-page document
A single HTML document can contain one or many "page" containers simply by stacking multiple divs with a data-role of "page". This allows you to build a small site or application within a single HTML document; jQuery Mobile will simply display the first "page" it finds in the source order when the page loads.

If a link in a multi-page document points to an anchor (#foo), the framework will look for a page wrapper with that id (id="foo"). If it finds a page in the HTML document, it will transition the new page into view. You can seamlessly navigate between local, internal "pages" and external pages in jQuery Mobile. Both will look the same to the end user except that external pages will display the Ajax spinner while loading. In either situation, jQuery Mobile updates the page's URL hash to enable Back button support, deep-linking and bookmarking.

It's important to note that if you are linking from a mobile page that was loaded via Ajax to a page that contains multiple internal pages, you need to add a rel="external" or data-ajax="false" to the link. This tells the framework to do a full page reload to clear out the Ajax hash in the URL. This is critical because Ajax pages use the hash (#) to track the Ajax history, while multiple internal pages use the hash to indicate internal pages so there will be conflicts in the hash between these two modes.

For example, a link to a page containing multiple internal pages would look like this:

<a href="multipage.html" rel="external">Multi-page link</a>
发表于 2017-9-18 17:40:50 | 显示全部楼层
scarlett 发表于 2017-9-18 15:53
$('#login').on('click', function () {
            // get the value from the username field
       ...

http://"+ip+"/webticket/index.php/cuser 这个和当前浏览器地址栏里的 URL 一样么?如果一样直接用 location.reload()。如果不一样,尝试楼上的加随机数方法。
发表于 2017-9-13 13:48:07 | 显示全部楼层
js 是需要浏览器执行的,所以和 $this->load->view() 是没有任何关系的,你好像没有理解JS和PHP的关系,建议你说下需求,看看能不能给你个解决方案。
发表于 2017-9-13 15:26:59 | 显示全部楼层
$this->load->view(),执行时,

在浏览器里,按 F12,看下 JS 报错信息。

感觉这是 JS问题,或者说是文件引入的问题。
 楼主| 发表于 2017-9-14 16:05:11 | 显示全部楼层
需求是这样的:
登录页面登录成功后,跳转到用户页面,用户页面加载时先检查是不是已登录,如果已登录就显示用户名,隐藏“请先登录”按纽。如果没有登录就显示“请先登录”按钮,隐藏“用户名”、“退出”按纽。

可是我现在遇到 的问题是登录成功后,跳转过去会显示如下图片,onload函数没有调用。
user.PNG
 楼主| 发表于 2017-9-14 16:07:02 | 显示全部楼层
刷新后就又显示正确了。
user1.PNG
发表于 2017-9-15 09:02:36 | 显示全部楼层
你的情况可能是缓存问题,你尝试,在登录成功后,页面如果需要跳转的话,附加些随机字符。以便不缓存这个页面。

发表于 2017-9-15 11:43:36 | 显示全部楼层
scarlett 发表于 2017-9-14 16:05
需求是这样的:
登录页面登录成功后,跳转到用户页面,用户页面加载时先检查是不是已登录,如果已登录就显 ...

登录成功后,你是用的什么方式跳转的?贴你的跳转代码看一下。
发表于 2017-9-18 16:40:13 | 显示全部楼层
JS复制代码
 
#跳转链接加个随机数呢???
location.href= "http://"+ip+"/webticket/index.php/cuser?time="+Math.random() ;
 
复制代码

本版积分规则