最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Window onload function is not working properly? - Stack Overflow

programmeradmin10浏览0评论

This is my code :

I have used window onload function to populate ui elements but it is not working properly.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Regalo - Shop</title>
<link rel="stylesheet" href="<%=request.getContextPath()%>/resources/css/bootstrap.min.css" />
<script src="<%=request.getContextPath()%>/resources/js/jquery-1.9.1.js"></script>
<script type="text/javascript" src=".10.2.min.js"></script>
<script type="text/javascript">

$(window).on('load',function(){
    alert("OK");
    <c:forEach var="item" items="${itemList}">

            var name= '${item.desktopbrand}';
            var details='${item.details}';
            var id = '${item.desktopid}';
            var price = '${item.desktopid}';
            var catagory = '${item.catagory}';          

    var spanTag = '<div class="col-sm-6 col-md-4"> <div class="thumbnail"> <img data-src="img/Desktop/'+ id +'.jpg"> <div class="caption"><h3>'+ name+ '</h3> <p>'+ price+ '</div> </p> <p><button type="button" class="btn btn-success btn-lg" onclick="details()">Show Details</button></p><p><button type="button" class="btn btn-success btn-lg" onclick="addToCart()">Buy now</button></p></div></div></div>';
    $("#" + catagory).append(spanTag);
        </c:forEach>
    });
</script>
</head>
<body>
    <h1>Gens Cooner</h1>
    <div id="gen" class="container"></div>
    <h1>Ladies Cooner</h1>
    <div id="lad" class="container"></div>
    <h1>Childrens Cooner</h1>
    <div id="chil" class="container"></div>
</body>
</html>

I try to find the issue but couldn't. Your help would be highly appreciated.

This is my code :

I have used window onload function to populate ui elements but it is not working properly.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Regalo - Shop</title>
<link rel="stylesheet" href="<%=request.getContextPath()%>/resources/css/bootstrap.min.css" />
<script src="<%=request.getContextPath()%>/resources/js/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://code.jquery./jquery-1.10.2.min.js"></script>
<script type="text/javascript">

$(window).on('load',function(){
    alert("OK");
    <c:forEach var="item" items="${itemList}">

            var name= '${item.desktopbrand}';
            var details='${item.details}';
            var id = '${item.desktopid}';
            var price = '${item.desktopid}';
            var catagory = '${item.catagory}';          

    var spanTag = '<div class="col-sm-6 col-md-4"> <div class="thumbnail"> <img data-src="img/Desktop/'+ id +'.jpg"> <div class="caption"><h3>'+ name+ '</h3> <p>'+ price+ '</div> </p> <p><button type="button" class="btn btn-success btn-lg" onclick="details()">Show Details</button></p><p><button type="button" class="btn btn-success btn-lg" onclick="addToCart()">Buy now</button></p></div></div></div>';
    $("#" + catagory).append(spanTag);
        </c:forEach>
    });
</script>
</head>
<body>
    <h1>Gens Cooner</h1>
    <div id="gen" class="container"></div>
    <h1>Ladies Cooner</h1>
    <div id="lad" class="container"></div>
    <h1>Childrens Cooner</h1>
    <div id="chil" class="container"></div>
</body>
</html>

I try to find the issue but couldn't. Your help would be highly appreciated.

Share Improve this question edited May 1, 2014 at 10:39 Janak 5,0524 gold badges28 silver badges45 bronze badges asked May 1, 2014 at 10:12 user2810351user2810351 351 gold badge2 silver badges11 bronze badges 4
  • 1 Don't try to debug JavaScript by looking at server side code. – Quentin Commented May 1, 2014 at 10:13
  • Look at the JavaScript that your server side code outputs. Determine why it doesn't work. Then either just fix that, or determine why the server side code doesn't give you the output you think it should. – Quentin Commented May 1, 2014 at 10:14
  • 1 Is there any particular reason you're not using your server-side code to generate HTML contents, rather than haxing in some JavaScript instead? – Niet the Dark Absol Commented May 1, 2014 at 10:15
  • Agree with what @Quentin said but you could also try using $(document).ready(function(){... – Rob Schmuecker Commented May 1, 2014 at 10:15
Add a ment  | 

1 Answer 1

Reset to default 2
window.onload = function(){ alert('Working!!'); }

Use above function and make sure you use only one window.onload function in a page

发布评论

评论列表(0)

  1. 暂无评论