Javascript JS文件未加载第一页加载?

Javascript JS文件未加载第一页加载?,javascript,jquery,html,css,Javascript,Jquery,Html,Css,在文件(index.shtml)上执行脚本include时,页面不会执行它应该执行的任务(在页面底部显示一个div) 我必须重新加载页面,脚本才能生效。如何第一次加载脚本?以前从未有过这个问题 要显示的脚本: document.write('<style type="text/css">a{text-decoration: none;}#cookieMessage {font-family: Arial, Helvetica, sans-serif;color: #222;bac

在文件(index.shtml)上执行脚本include时,页面不会执行它应该执行的任务(在页面底部显示一个div)


我必须重新加载页面,脚本才能生效。如何第一次加载脚本?以前从未有过这个问题

要显示的脚本:

document.write('<style type="text/css">a{text-decoration: none;}#cookieMessage {font-family: Arial, Helvetica, sans-serif;color: #222;background-color: #D1D3D4;padding: 8px;height: auto;position: fixed;width:200px;margin: 0px 0px 0 0;bottom:0px;right:10px;border:#FFF 2px solid;border-bottom:0px;padding: 0px 8px 8px 8px;-moz-border-radius-topleft: 8px;-moz-border-radius-topright: 8px;-webkit-border-top-left-radius: 8px;-webkit-border-top-right-radius: 8px;border-top-right-radius: 8px;border-top-left-radius: 8px;text-align:left;overflow:hidden;z-index:999999;}#cookieMessage h6 {font-size: 14px;text-transform: uppercase;padding: 0px;margin-top: 8px;margin-right: 0px;margin-bottom: 5px;margin-left: 0px;color: #222;}#cookieMessage img {border:0px;}#cookieMessage p {text-align: left;font-size: 11px;padding: 0px;margin-top: 0px;margin-right: 0px;margin-bottom: 2px;margin-left: 0px;color: #222;}#cookieMessage p a {color: #333;}#cookieMessage h6 a {color: #333;text-decoration: none;}#arrow{margin-top: -20px;}</style>');
$(document).ready(function(){
    $("#polup").toggle(function(){
        $("#cookieMessage").animate({height:120},500);
        $("#arrow").animate({height:0},160);
    },function(){
        $("#cookieMessage").animate({height:22},500);
        $("#arrow").animate({height:14},160);
    });
});

var accepted = get_cookie("accepted");
if(accepted == null){
    document.cookie = "accepted=no; path=/"; // initialize the cookie
}

function accept(){
    // when the client clicks accept
    $("#cookieMessage").fadeOut();
    $.cookie("accepted", "yes", {
       expires : 360,           //expires in 10 days
       path    : '/',          //The value of the path attribute of the cookie 
    });
}
function get_cookie ( cookie_name )
// method to retrieve a cookie based upon its name entered
    {
      var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

      if ( results )
        return ( unescape ( results[2] ) );
      else
        return null;
    }
var x = get_cookie("consentCookie");

        if((x == null) && (accepted == "no")){
        //if the cookie does not exist it is the users first time on the page
        document.cookie = "consentCookie=no; path=/";
        var policy = '<div id="cookieMessage"><h6>Cookie Policy</h6><p>Our website uses cookies. By using our website and agreeing to this policy, you consent to our use of cookies. <a href="cookie_policy.php">Find out more about cookies.</a></p><p><a href="#" onclick="accept();"><img src="cookiesJs/cookieimg/cookie.gif" width="20" height="20" alt="cookie" style="margin-bottom:-5px;" /> I accept</a></p></div>';
        document.write(policy);
    }else if((x == "no") && (accepted == "no")){
        // if they have visited before but not accepted
        document.write('<div id="cookieMessage">');
        var policy = '<a href="#_" id="polup"><h6>Cookie Policy</h6> <img id="arrow" align="right" src="cookiesJs/cookieimg/arrow.gif" width="18" height="14" alt="arrow" /></a></h6>';
        document.write(policy);
        document.write('<p>Our website uses cookies. By using our website and agreeing to this policy, you consent to our use of cookies.</p><p><a href="cookie_policy.php">Find out more about cookies.</a></p><p><a href="#" onclick="accept();"><img src="cookiesJs/cookieimg/cookie.gif" width="20" height="20" alt="cookie" style="margin-bottom:-5px;" /> I accept</a></p></div>');
        $("#cookieMessage").css("height","20");
    }else{
        //if they  have visited and accepted
        document.write('<div id="cookieMessage" style="display: none"><h6>Cookie Policy</h6></div>');
    }
document.write({text-decoration:none;}#cookieMessage{字体系列:Arial,Helvetica,无衬线;颜色:#222;背景色:#D1D3D4;填充:8px;高度:自动;位置:固定;宽度:200px;边距:0px 0px 0;底部:0px;右侧:10px;边框:#FFF 2px实心;边框底部:0px;填充:0px 8px 8px;moz边框半径上左:8px;-moz边框半径上右:8px;-webkit边框顶部左上半径:8px;-webkit-border-top-right-radius:8px;border-top-right-radius:8px;border-left-radius:8px;文本对齐:左;溢出:隐藏;z-index:999999;}cookieMessage h6{字体大小:14px;文本转换:大写;填充:0px;边距顶部:8px;边距右侧:0px;边距底部:5px;边距左侧:0px;颜色:}cookieMessage img{边框:0px;}{文本对齐:左;字体大小:11px;填充:0px;页边距顶部:0px;页边距右侧:0px;页边距底部:2px;页边距左侧:0px;颜色:}cookieMessage p a{颜色:#333;}cookieMessage h6 a{颜色:#333;文本装饰:无;}箭头{页边距顶部:-20px;};
$(文档).ready(函数(){
$(“#polup”).toggle(函数(){
$(“#cookieMessage”).animate({height:120},500);
$(“#箭头”).animate({height:0},160);
},函数(){
$(“#cookieMessage”).animate({height:22},500);
$(“#箭头”)。制作动画({height:14},160);
});
});
var accepted=获取cookie(“accepted”);
如果(已接受==null){
document.cookie=“accepted=no;path=/”;//初始化cookie
}
函数accept(){
//当客户端单击accept时
$(“#cookieMessage”).fadeOut();
$.cookie(“接受”、“是”{
过期时间:360,//10天后过期
路径:“/”,//cookie的path属性的值
});
}
函数get\u cookie(cookie\u名称)
//方法根据输入的cookie名称检索cookie
{
var results=document.cookie.match(“(^ |”)?“+cookie_name+”=([^;]*)(;|$);
如果(结果)
返回(unescape(结果[2]);
其他的
返回null;
}
var x=获取cookie(“同意cookie”);
如果((x==null)&&(接受==no”)){
//如果cookie不存在,则是用户第一次出现在页面上
document.cookie=“同意cookie=no;路径=/”;
var policy='Cookie policy我们的网站使用Cookie。通过使用我们的网站并同意此策略,您同意我们使用Cookie。

'; 文件编写(政策); }如果((x==“否”)&&(接受==“否”)){ //如果他们以前访问过但未接受 文件。写(“”); var策略=“”; 文件编写(政策); 文档。写入(“我们的网站使用cookies。通过使用我们的网站并同意此政策,即表示您同意我们使用cookies。

”; $(“#cookieMessage”).css(“高度”、“20”); }否则{ //如果他们参观并接受了 编写(“Cookie策略”); }
脚本真的没有被执行吗?如果您收到警报框,您是否尝试过使用警报


在我看来,您的问题可能与这样一个事实有关:第一次加载页面时没有cookie,您在代码中设置了cookie,但我相信您只能在下次加载页面时读取cookie。您可以检查cookie,如果没有cookie,则设置cookie,以编程方式重新加载并运行其余代码。

脚本是什么t应该添加你的div?编辑的op应该在post的底部嘿,是的,你的权利。难以置信我没有检查页面是否实际加载!谢谢你的评论。没问题,很高兴我能帮上忙!
document.write('<style type="text/css">a{text-decoration: none;}#cookieMessage {font-family: Arial, Helvetica, sans-serif;color: #222;background-color: #D1D3D4;padding: 8px;height: auto;position: fixed;width:200px;margin: 0px 0px 0 0;bottom:0px;right:10px;border:#FFF 2px solid;border-bottom:0px;padding: 0px 8px 8px 8px;-moz-border-radius-topleft: 8px;-moz-border-radius-topright: 8px;-webkit-border-top-left-radius: 8px;-webkit-border-top-right-radius: 8px;border-top-right-radius: 8px;border-top-left-radius: 8px;text-align:left;overflow:hidden;z-index:999999;}#cookieMessage h6 {font-size: 14px;text-transform: uppercase;padding: 0px;margin-top: 8px;margin-right: 0px;margin-bottom: 5px;margin-left: 0px;color: #222;}#cookieMessage img {border:0px;}#cookieMessage p {text-align: left;font-size: 11px;padding: 0px;margin-top: 0px;margin-right: 0px;margin-bottom: 2px;margin-left: 0px;color: #222;}#cookieMessage p a {color: #333;}#cookieMessage h6 a {color: #333;text-decoration: none;}#arrow{margin-top: -20px;}</style>');
$(document).ready(function(){
    $("#polup").toggle(function(){
        $("#cookieMessage").animate({height:120},500);
        $("#arrow").animate({height:0},160);
    },function(){
        $("#cookieMessage").animate({height:22},500);
        $("#arrow").animate({height:14},160);
    });
});

var accepted = get_cookie("accepted");
if(accepted == null){
    document.cookie = "accepted=no; path=/"; // initialize the cookie
}

function accept(){
    // when the client clicks accept
    $("#cookieMessage").fadeOut();
    $.cookie("accepted", "yes", {
       expires : 360,           //expires in 10 days
       path    : '/',          //The value of the path attribute of the cookie 
    });
}
function get_cookie ( cookie_name )
// method to retrieve a cookie based upon its name entered
    {
      var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

      if ( results )
        return ( unescape ( results[2] ) );
      else
        return null;
    }
var x = get_cookie("consentCookie");

        if((x == null) && (accepted == "no")){
        //if the cookie does not exist it is the users first time on the page
        document.cookie = "consentCookie=no; path=/";
        var policy = '<div id="cookieMessage"><h6>Cookie Policy</h6><p>Our website uses cookies. By using our website and agreeing to this policy, you consent to our use of cookies. <a href="cookie_policy.php">Find out more about cookies.</a></p><p><a href="#" onclick="accept();"><img src="cookiesJs/cookieimg/cookie.gif" width="20" height="20" alt="cookie" style="margin-bottom:-5px;" /> I accept</a></p></div>';
        document.write(policy);
    }else if((x == "no") && (accepted == "no")){
        // if they have visited before but not accepted
        document.write('<div id="cookieMessage">');
        var policy = '<a href="#_" id="polup"><h6>Cookie Policy</h6> <img id="arrow" align="right" src="cookiesJs/cookieimg/arrow.gif" width="18" height="14" alt="arrow" /></a></h6>';
        document.write(policy);
        document.write('<p>Our website uses cookies. By using our website and agreeing to this policy, you consent to our use of cookies.</p><p><a href="cookie_policy.php">Find out more about cookies.</a></p><p><a href="#" onclick="accept();"><img src="cookiesJs/cookieimg/cookie.gif" width="20" height="20" alt="cookie" style="margin-bottom:-5px;" /> I accept</a></p></div>');
        $("#cookieMessage").css("height","20");
    }else{
        //if they  have visited and accepted
        document.write('<div id="cookieMessage" style="display: none"><h6>Cookie Policy</h6></div>');
    }