- Feb 8, 2004
- 12,604
- 15
- 81
I give up, ive been at this for ages, what am I missing here? Im trying to set the action attribute of a form using javascript. It works when I use a string literal, it works when I use a variable declared in the same html file, it absolutely will not accept variables from another file... I dont understand why. Ive even dumped a copy of the ServerAPI.js file right there in the same directory to eliminate the possibility of it not being found.
HTML file, ive bolded the relevant bits:
ServerAPI.js, the file I want that first html document to be able to read variables from
What im doing works fine in index.html though:
Im certain ive missed something or theres something im not understanding about how this is meant to work
HTML file, ive bolded the relevant bits:
Code:
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
[B] <script type="text/javascript" src="ServerAPI.js"></script>
[/B] <div>Session test page! Should only appear for a valid session.</div>
<form id="goToMainPageForm" action="not fucking working" method="GET">
<input type="submit" value ="Back to main page">
<input type="hidden" name="jsessionid">
</form>
<form name="logoutForm" action="/fitness_tracker_servlet_maven/FrontController/desktopLogout" method="POST">
<input type="submit" value ="Logout">
<input type="hidden" name="jsessionid">
</form>
<script type="text/javascript">
//here we get the jsessionID if there is one
window.onload = function()
{
var currentURL = window.location.href;
var jSessionIDSearchParameter = ";jsessionid=";
var jSessionIDLocation = currentURL.search(jSessionIDSearchParameter);
if(jSessionIDLocation === -1)
{
//no jSessionID found
}
else
{
var jSessionID = currentURL.substring(jSessionIDLocation);
var elementsList = document.getElementsByName("jsessionid");
for(var count = 0; count < elementsList.length;count++)
{
elementsList[count].value = jSessionID;
}
}
};
</script>
[B] <script type="text/javascript">
window.onload = function()
{
document.getElementById("goToMainPageForm").action = desktopMainPageRequest;
};
</script>[/B]
</body>
</html>
ServerAPI.js, the file I want that first html document to be able to read variables from
Code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//parameters
var port = 8080;
var projectName = "fitness_tracker_servlet_maven";
var mainURL = "http://localhost:"+ port + "/" + projectName + "/";
//Server API
var URLLoginPage = "index.html";
//FrontController
var frontController = "/fitness_tracker_servlet_maven/FrontController/";
//desktop
var desktopLoginPageRequest = "desktopLoginPage";
var desktopLoginRequest = "desktopLogin";
var desktopLogoutRequest = "desktopLogout";
var desktopMainPageRequest = "desktopMainPage";
var desktopSessionPlaceholderRequest = "desktopSessionPlaceholder";
//mobile
var mobileLoginPageRequest = "mobileLoginPage";
var mobileLoginRequest = "mobileLogin";
var mobileLogoutRequest = "mobileLogout";
var mobileMainPageRequest = "mobileMainPage";
var mobileSessionPlaceholderRequest = "mobileSessionPlaceholder";
var alreadyLoggedIn = "alreadyLoggedIn";
What im doing works fine in index.html though:
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<script type="text/javascript" src="ServerAPI.js"></script>
//method testing the userAgent to check for mobile devices, this method
//accounts for tablets therefore is preferred
window.onload = function()
{
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) )
{
window.location="FrontController/" + mobileLoginPageRequest;
}
else
{
window.location="FrontController/" + desktopLoginPageRequest;
}
};
</script>
</head>
<body>
</body>
</html>
Im certain ive missed something or theres something im not understanding about how this is meant to work
