Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Javascript as page opens

I have a little problem, I want to run javascript as soon as page opens, however it does not work for me. On many forums and tutorials I tried they suggest using onload function in my case:

<body onload="loadPage()">  

so my script is called loadPage, however it does not work... Instead of opening one of the pages(the links in if, else if statements) - a just have a blank page. Any help greatly appriciated!

<%@ include file="header.jsp" %> 
<%@ page import="java.sql.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ page import="java.util.ArrayList" language="java" %>
<%@ page import="java.text.MessageFormat" language="java" %>
<%@ page import="java.util.List" language="java" %>
<%@ page import="java.util.ArrayList" %>

<%
    String place = request.getParameter("place");
%>
<html>
<head>
<script type="text/javascript">
function loadPage() 
{
    if( <%=place%> == "birr")
    {
       window.open("http://localhost:82/IrishClimateData/Birr.jsp");
    }

    ...more else if statemens here...   

    else if( <%=place%> == "Shannonairport")
    {
       window.open("http://localhost:82/IrishClimateData/Shannon airport.jsp");
    }
}
</script>
</head>
<body onload="loadPage()">  
</body>
</html>

I played around with different options and did the following:

var placeName = "<%=place%>";
if( placeName == "birr")

I also had to add ; at the end of function. I thought apache tomcat would actually point it to me - as it usually does, but it did not... Thanks to all anyway!

like image 711
Andrei Ivanov Avatar asked Jul 09 '26 09:07

Andrei Ivanov


1 Answers

After removing your server side code and simplifying your loadPage function, it looks like it will run fine. Here is a live example: http://jsbin.com/ozetah/2/?place=birr

The live example code:

<html>
<head>
<script type="text/javascript">
function loadPage() {
  // code to replicate server side capture of GET parameter
  var place = /place=(.*?)(?:$|&)/.exec(document.location.search);
  if( place.length >1 ) { place = place[1]; }


  if( place == "birr") {
    window.open("http://localhost:82/IrishClimateData/Birr.jsp");
  } else if( place == "Shannonairport") {
    window.open("http://localhost:82/IrishClimateData/Shannon airport.jsp");
  } else {
    alert('place param of "'+place+'" doesn\'t match');
  }
}
</script>
</head>
<body onload="loadPage()">  
</body>
</html>

Since all of this works and you are not seeing any errors in your code, I would check the html output to make sure your server side code is outputting what you would expect. You can take out all of the server side code and start adding pieces back in to see what breaks it.

like image 72
Mike Grace Avatar answered Jul 11 '26 22:07

Mike Grace



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!