I can not get the button clicked when i insert it inside an echo..
echo '<input onclick="window_target = document.basicForm.maparea1;
window_name = window.open("search.php",
"window_name",
"resizable=0,location=0,directories=0,status=0,top=30,left=100,toolbar=no,width=180,height=480,menubar=no,scrollbars=no"
);
window_name.window_target = window_target
" value="Select" type="button">';
-
2Invalid quoting in the echoed tag.Teemu– Teemu2014年09月19日 20:11:08 +00:00Commented Sep 19, 2014 at 20:11
3 Answers 3
welcome to modern JS: try not to use onclick, or in fact any on... handler embedded in the HTML directly. Instead, generate your page to do something like:
<!doctype html>
<html>
<head>...</head>
<body>
<input type="..." id="spawnClickerThing" ...>
...
<script src="mypagescript.js"></script>
</body>
</html>
(so not PHP echo/printed!) and then in that javascript include, do all the event binding etc:
...
function newWindowHandling(avt) {
var window_target = document.basicForm.maparea1;
var window_name = window.open("search.php", "window_name", "...");
window_name.window_target = window_target;
}
function setEverythingUp() {
document.removeEventListener("DOMContentLoaded", setEverythingUp);
var inputclicker = document.querySelector("#spawnClickerThing");
inputclicker.addEventListener("click", newWindowHandling);
}
document.addEventListener("DOMContentLoaded", setEverythingUp);
depending on whether you use libraries for more efficient JavaScript, that code can be compacted, but the general principle holds. Don't inject JS "inline" using PHP, it's extremely fragile and impossible to maintain. Keep your script in .js files that are easy to edit, independently of the .php scripts that generate the HTML source code for your pages.
Comments
escape the quotes
echo '<input onclick="window_target = document.basicForm.maparea1; window_name = window.open(\'search.php\', \'window_name\', \'resizable=0,location=0,directories=0,status=0,top=30,left=100,toolbar=no,width=180,height=480,menubar=no,scrollbars=no\'); window_name.window_target = window_target" value="Select" type="button">';
1 Comment
you can't this way for this to work you have to use
you have to use like this in your javascript .i am assuming you have jquery included
$(document).on('click', '.btninline' ,function() {
window_target = document.basicForm.maparea1;
window_name = window.open("search.php", "window_name","resizable=0,location=0,directories=0,status=0,top=30,left=100,toolbar=no,width=180,height=480,menubar=no,scrollbars=no");
window_name.window_target = window_target;
}
and then in your php
echo '<input class="btninline" value="Select" type="button">';