I can't get my Javascript to execute.It randomly stopped working yesterday and I've been messing around to try to get it to work. I tried making a simple code to see if it would work but, I have no luck. Here is the code:
HTML:
<script type="text/javascript" src="js\hello.js"></script>
<form id="hello" onsubmit="return hello();">
<input type="submit" />
</form>
Javascript (in sub folder called js):
function hello()
{
alert("HELLO");
return false;
}
It's really annoying me because all of my Javascript codes aren't working but they were working perfectly fine yesterday. I am not sure what happened...
I meant to make an alert I was just wasn't thinking. It still doesn't work anyways. It's like it isn't even calling the function.
use alert("HELLO"); instead of echo. echo is PHP not javascript.
If you want to output to a speicific tag then use document.getElementById("idOfTag").innerHTML = "HELLO";
Related
Friends recently i was writing code on jsp where i have to use a button to call a class and use its method.However when i tried same i received unexpected output.I tried search the reason and came across various related questions but i am still struck as i don want use EL or jstl.so basically when i am calling a javascript function on button click it is working but as soon as i put some java code lets say response.sendRedirect("/abc.jsp") the page is getting redirected even before i click button.Please help as i am really struck and not able to get solution.Below is working fine.But when I add any Java code.It is performed without click event.
</head>
<body>
<button id="doUnzip" type="submit" name="unzip">Unzip</button>
<script>
document.getElementById('doUnzip').onclick=function (){
alert("welocome");
};
</script>
</body>
Now the below is not working
<body>
<button id="doUnzip" type="submit" name="unzip">Unzip</button>
<script>
document.getElementById('doUnzip').onclick=function (){
<%
response.sendRedirect("/index.jsp");
%>
};
</script>
</body>
I know this question has been asked a lot, but none of the solutions to the question have worked for me. This is for a Google Chrome Extension. I am calling a javascript function in an html file like this:
<div style="text-align:center">
<input type="button" onClick="buttonClicked()" value="Activate Extension">
</div>
<script type="text\javascript" src="eventPage.js"></script>
</body>
</html>
in eventPage.js:
function buttonClicked() {
var uname = document.getElementById("uname").value;
alert("Extension activated. Enjoy!");
}
I have tried moving the html tag in the head and the body and nothing changes. Any and all help is greatly appreciated.
Changing the type should work. If not check if the external reference path is correct and all the javascript is available.
Also check if you see any errors in developer console.
Try to add some breakpoints and see if that function is being called.
It's because of this line var uname = document.getElementById("uname").value; where you are giving a variable to the button value.It is only needed when there is input type "text" where there will be text in the text box.
Your code should be following and it works fine.
<div style="text-align:center">
<input type="button" onClick="buttonClicked()" value="Activate Extension">
</div>
<script src="eventPage.js"></script>
</body>
Just remove variable line from the javascript.
function buttonClicked() {
alert("Extension activated. Enjoy!");
}
I have a website with lots of code, so obviously something is wrong, but I can't post all the code here. Here's the exact code I'm trying to use though.
Parent document...
<iframe name=combosIframe id=combosIframe src=getCombos.php style='position:absolute;left:300px;top:0px;width:275px;height:520px;overflow-y:scroll;overflow-x:hidden;border:1px solid black'></iframe>
<form name=colorForm id=colorForm action=colorCombos.php method=post target=combosIframe>
<input type=submit id=submitColors value=SubmitColors>
</form>
Inside the iframe, colorCombos.php page...
<script language=JavaScript>
alert('load');
function doSomething(){
alert('hi');
}
</script>
<form>
<input type=button value=ClickMe onClick=doSomething()>
</form>
Scenario: I click the parent form button to load the new page into the iframe.
Problem: the JavaScript in the colorCombos.php page does not alert, either on page load, or when clicking the button. If I view the page on its own it works fine.
Specs: php5, using Chrome browser.
Of note: there are no errors when I view the javascript console. Also there is nothing else on the colorCombos.php page. Both files are in the same directory. Thanks for any help, I'm really stumped.
Not using quotes within HTML Attributes can have undesired effects. Try adding quotes around all the values of each HTML Attribute.
Example:
<input type="button" value="ClickMe" onClick="doSomething()"/>
I started with a blank setup (which was working) copied the code from my site (which has lots of other code), bit by bit into the new document to see when the script stopped working. I ended up copying EVERYTHING (literally ctrl+a) into the new document, and it worked fine. I then renamed the new document to be the name of the old document, and it STOPPED working. The old document (i.e. the parent document) is named rangeTool.php, not that I think that is relevant necessarily.
So I have a solution, but not an answer. What in the world could cause one document to not work? Something wrong on the server? I deleted the old file off the server and loaded the new copy, but that didn't work as long as the name was rangeTool.php. For example, I renamed it testcode.php and it worked fine.
I'm still interested in why/how this happened, but at least the pressure is off now that it's working for me.
I have been reading to stay away from JavaScript for a lot of things, however I was messing around with it and it doesnt seem to work (unless I am blind and have missed something)
<SCRIPT LANGUAGE="text/javascript">
function testResults() {
System.out.println("Got into function");
}
</SCRIPT>
And this is my body:
<form action="" method="GET">
<input type="button" value="Save" name="saveType" style="margin:10px;" onClick="testResults()"/>
</form>
From what I have seen of javascipt this should be fine...
Any suggestions?
Use console.log instead of system.out.println...
Javascript is not Java!
A few pointers:
it's type="text/javascript"
why capital letters?
System.out.println is Java
Stay away from JavaScript? Currently it's almost like the opposite, but as almost always it depends on what you have to do :)
to get it work use this instead:
Look here - jsfiddle
<form action="" method="GET">
<input type="button" value="Save" name="saveType" style="margin:10px;" onClick="testResults()"/>
</form>
<script type="text/javascript">
function testResults() {
alert("Got into function");
}
</script>
it is type="text/javascript and not language="text/language" also in javascript you have to use alert().
Javascript doesn't have a standard output like that. You have a few options:
If you have a debugging console running, you can use console.log("log a message")
You can output debug information to a HTML element: document.getElementById("myelement").innerHTML = "message here";
There are loads more options - I'd suggest learning Javascript properly. It's not that much like Java.
Java => System.out.println or System.out.print
Javascript => console.log
:)
I have written this code for Firefox:
<html><head><title>No</title>
<script type="text/javascript" src="jquery-1.4.2.js"></script>
</head>
<body>
<form action="javascript:void(alert('Yes'));">
<input type="submit" value="Submit">
</form>
<script>$($('form').submit())</script></body></html>
It correctly displays the alert box.
However, when i run this inside an iframe, with this code:
<html><body><iframe src="click.php"></iframe></body></html>
i don't get the alert box, not even if i click the submit button myself.
What is going on exactly? The same code works in Chromium
Well, don't do that then!
It doesn't make any sense to submit a form to a javascript: URL. Use a submit event handler to pick up the form submission and execute script, eg using jQuery:
$('#someform').submit(function() {
alert('Yes');
return false;
});
A good rule of thumb about when to use javascript: URLs is: never.
It looks like it's a problem with FF4 so I'll discuss it on their bugzilla if it's really their fault. I have modified the source so I'm not even sure it is a bug...