Scripplets not working inside a javascript function - javascript

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>

Related

Handling form content with JavaScript and Perl

I have a music project in Perl which also requires some JavaScript but I seem to be stuck.
I need to execute a program (ffplay) as a command-line application so it runs without displaying a GUI window. The Perl handles the server end of things (sqlite database access). I need JavaScript because I display track names as a button, which when clicked is supposed to run 'ffplay' to play the track named in the button code. But the button click requires JavaScript.
When I click a track name button, it only displays the first track, no matter which one I
click.
The following code extracts track names from the DB.
$sth=$dbh->prepare (qq{select track from tracks where
artistid=$ArtistID and cdid=$cdID order by track});
$sth->execute();
while(#records=$sth->fetchrow_array()) {
$Track=$records[0];
print<<EndHTML;
<script>
var form=window.document.forms[0]
function process(form) {
alert ("Track: " + form.elements.Track.value)
}
</script>
EndHTML
print qq{<form name="Player">};
print qq{<input type="button" onClick="process(this.form)" name="Track" value="$Track"><br/>};
print qq{</form>};
}
$sth->finish();
SOLVED.
Changed the JavaScript function :
<html lang="en">
<head>
<script>
function process(value) {
var form =document.Player
alert ("Track: " + value)
}
</script>
</head>
<body>
<form name="Player">
<button onClick="process(this.value)" name="Track1" value="[01] Evenfall.flac">[01] Evenfall.flac</button><br/>
<button onClick="process(this.value)" name="Track2" value="[02] 6am.flac">[02] 6am.flac</button><br/>
<button onClick="process(this.value)" name="Track3" value="[03] Cut.flac">[03] Cut.flac</button><br/>
</form>
</body>
Now the alert correctly tells me which button was clicked.

How to show a text on the same webpage by pressing a button, button is on a popup on Folium map

As you all know we can print a text on the same webpage by this code on the HTML:
<body>
<button onclick="myFunction()">Click me</button>
<h1 id="myHeader"></h1>
<script>
function myFunction()
{
document.getElementById("myHeader").innerHTML = "hi";
}
</script>
</body>
In my code the button is on the popup on the Folium map which looks like this:
So, I would like to write "hi" on the h1 tag, onclick function for this button "More info!" on the same page:
in HTML file:
<h1 id="myHeader">SSSSSS</h1>
<div>
<form name="AAA" >
<center>
{{ map| safe }}
</center>
</form>
</div>
<script>
function showsth()
{
document.getElementById("myHeader").innerHTML = "hi";
}
</script>
And I am using Django, so, the I predefined the popup like this:
In views.py:
popup_t = (...
+ '<button onclick="showsth()">More info!</button>'
)
test = folium.Html(popup_t, script=True)
popup = folium.Popup(test, min_width=150, max_width=500)
So, when I click on the button, I didn't get any response just like I showed in the first HTML code.
Do you have any idea about the problem?
UPDATE
I tested the AJAX JQUERY as well.
the button outside the folium's popup will work onclick, but when the button is transferred to the popup, it would not work.
It came to my mind that maybe I can work with "link", instead of "button".
The question is that: is it possible to have a link (href) that direct user to the same page without reloading? just like what AJAX does for buttons!
After a lot of research, I concluded that it is impossible to get a response from the Folium map click. For me, I used Leaflet and Ajax jQuery. I defined the map on the HTML file and used a function on-clicking the geo points. The function returned the point data to the views.py, some works were done, and the response was sent back to the HTML file in jQuery's success function. You can follow my code lines in my other qs:
How to send a simple data by Ajax jQuery to views.py in Django?

How to create an HTML button that show more text same page

I'm working with html and javascript. My problems is, in one webpage a show a plot and a few button. When the user press any of this button I need show 3 or 4 options but in the same page without switching pages.
Below is my code
<form action="MyPage">
<button type="submit" value="More Options">
</form>
redirect to an other page.What I can do?
First of all, get rid of type="submit". That's what's causing the page to do stuff you don't want. The second thing is to add an onclick handler. It should return false to avoid behavior like "submit". The variable 'this' will pass the button to your function, which you might need in that code. Then fill in the body of addMoreStuff() with your code to, well, add more stuff!
<form action="MyPage">
<button onclick="addMoreStuff(this); return false; ">More Options</button>
</form>
<script type="text/javascript">
function addMoreStuff(button) {
/* your code here */
}
</script>
Drop the form (use the button alone), and look into jQuery. It's extremely easy to use, and it'll help you quickly build code your application.
HTML
<button type="submit" value="More Options" id="more">
JavaScript (jQuery)
// run "add_options" when someone clicks on the button
jQuery('button#more').on('click', add_options)
function add_options() {
//code here to add more options
}

Javascript randomly stopped working

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";

Javascript URL inside an iframe does not execute in Firefox

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...

Categories

Resources