Syntax issue at javascript, multiple functions in one file - javascript

Totally noob question over here, I have this JS code where using only ONE function works perfectly, but adding a second one makes it stop working.
function PSN() {
var myWindow = window.open("http://sintarjetas.com.ar/forms/psn.html", "Fran", "width=380, height=400");
}
function BLIZ() {
var myWindow = window.open("http://sintarjetas.com.ar/forms/bliz.html", "Fran", "width=380, height=400");
}
function XLA() {
var myWindow = window.open("http://sintarjetas.com.ar/forms/xla.html", "Fran", "width=380, height=400");
}
function STEAM() {
var myWindow = window.open("http://sintarjetas.com.ar/forms/steam.html", "Fran", "width=380, height=400");
}
PSN button works ok when there's only one function written, but as I put 2 or more, all of the links stop working.
Is it because of the syntax of this file? Or am I missing something?

you don't need multiple function to open pop up, you can just create single function and pass the url as param
function popUp(url) {
return window.open(url, "Fran", "width=380, height=400");
}
Note: Put your functions block in <header> of page then try. here is working fiddle
Update : Since all window names are same Fran you will need to close the opened window first before opening other. currently it will open the url in previously opened window.

Related

Call JavaScript function within document.write()

I am trying to call openswf function when i click on SWF part1 links which is in document.write(), but my problem is when i clicked on SWF part1 links it nothing happen.
This is my code.
<html>
show popup
<script>
function openswf(){
console.log("Do something")
}
function Popup()
{
var win = window.open('', '',"toolbar=no, width=1000, height=800");
var doc = win.document.open();
doc.write('SWF part1');
doc.close();
}
</script>
</html>
First i click on show popup links to call Popup() function and open new popup window which is work properly and then i want to click on SWF part1 links in that popup but it not working.
openswf is not defined in the popup window, but in the main window. But you can reference the main window with opener. I suppose you want to close the popup window when the user clicks:
doc.write('SWF part1');
It's probably calling the default <a> onClick event.
You have to preventDefault().
function Popup(event)
{
event.preventDefault();
var win = window.open('', '',"toolbar=no, width=1000, height=800");
var doc = win.document.open();
doc.write('SWF part1');
doc.close();
}
Let me know if it helps.
For starters, the variable "path" is not being passed to your new window. Use this instead...
function Popup(path)
{
var win = window.open('', '',"toolbar=no, width=1000, height=800");
var doc = win.document.open();
doc.write('SWF part1');
doc.close();
}

how to open link in external window with click on anchor tag with javascript?

This is probably such a noob question, but I can't get it to work.
I want to open external window link by clicking on anchor tag, but I keep getting error that myFunction() is not defined.
Open link
js
$(document).ready(function() {
$('#searchEng').click(function() {
const engine = document.getElementById('engine');
var en_ = engine.val();
if (en_ == "firefox")
{
function myFunction() {
var url = "https://www.mozilla.org/en-US/firefox/new/";
window.open(url,'_blank');
};
}
});
));
Why is it undefined?
I have .js included because other stuff works.
you can remove function inside click function
$(document).ready(function(){
$('#searchEng').click(function() {
const engine = document.getElementById('engine');
var en_ = engine.val();
if (en_ == "firefox")
{
var url = "https://www.mozilla.org/en-US/firefox/new/";
window.open(url,'_blank');
}
});
Open link
You declared myFunction inside another function, which makes it a local variable of that function. Local variables aren't available outside the function where they were defined.

IE11 - Popup window launched from script will not close using JavaScript

I am relatively new to JS so apologies for any basic errors I have made here.
I am attempting to insert some JS on our site that will detect if the user has Flash enabled & the site is able to launch a popup, if these fail the user will be directed to a support page to resolve these.
The code works without issue on Chrome and Firefox, the issue I am having is on IE the popup which launches as a test is not being closed by the script.
Am I missing something glaringly obvious?
function loadpopunder(){
var popupBlockerChecker = {
check: function(popup_window){
var _scope = this;
if (popup_window) {
if(/chrome/.test(navigator.userAgent.toLowerCase())){
setTimeout(function () {
_scope._is_popup_blocked(_scope, popup_window);
},250);
}else{
popup_window.onload = function () {
_scope._is_popup_blocked(_scope, popup_window);
};
}
}else{
_scope._displayError();
}
},
_is_popup_blocked: function(scope, popup_window){
if ((popup_window.outerHeight > 0)===true)
popup.close();
},
_displayError: function(){
popupFail=true;
}
};
var popup = window.open("http://www.google.com", '_blank', "width=10, height=10, left=1, top=1, scrollbars=no, resizable=no");
popupBlockerChecker.check(popup);
}
loadpopunder()

open window and run function on that window

I'd like to open a new window, this window has a list of objects, and these objects should be filtered based on a selection from the previous window. I figured I can filter the list through a function, but how do I run said function?
This is what I am able to do:
var popup = window.open('pageURL');
$(popup.document).ready(function() {
// this is where function should be
popup.alert('HelloWorld');
});
But how do I change the alert to a function?
If I have a function on my other app , function test() { alert('HelloWorld'};
How do I run this function from my first app?
Swapping popup.alert('HelloWorld'); with popup.test(); did not work.
You need the reference to the window opened to call functions in the new window, like:
var oNewWindow = window.open("new.window.url", "mywindow");
oNewWindow.onload = function(){oNewWindow.window.newWindowFunction();};
I ended up with this solution
var popup = window.open('http://s234-0057/actiontracker/SiteAssets/Avvik/html/app.aspx');
var readyStateCheckInterval = setInterval(function() {
if (popup.document.readyState === "complete") {
clearInterval(readyStateCheckInterval);
popup.test();
}
}, 50);
Where I check if the popup window is ready, and when it is, cancel check and run function. Solution is from top answer on this question, by #this.lau_
You can write it like this:
function myFunction(){
alert('HelloWorld');
}
var popup = window.open('pageURL');
$(popup.document).ready(function() {
popup.eval(myFunction + "");
popup.myFunction();
});
myFunction in file that contains this code will run in page with pageURL address.

Pop Up Blocker in Chrome and IE

Below is the piece of code I am using to open a link in a new window say "abc".
If the user again clicks on the same link, it should close and reopen the link in the same window "abc".
window.openOrFocus = function(url, "abc") {
if (!window.popups) {
window.popups = {};}
if (window.popups["abc"]){
var v=window.open("", "abc");
v.close();}
window.popups["abc"] = window.open(url, "abc");
}
But Now, say I click on the link, it opens the URL in a new window named "abc".
Now I go and close the window "abc". and go back and again click on the link.
That time it shows up the pop up blocker.
I am confused as to why this pop up blocker is coming when the I go and manually close the window and try to reopen by clicking on the link.
Happens both in IE as well as Chrome
Probably because you're calling window.open with a blank URL or repeatedly in that case.
You don't need your window.open("", "abc") call; instead, just use the window reference you already have:
window.openOrFocus = function(url, windowName) {
if (!window.popups) {
window.popups = {};
}
if (window.popups[windowName]){
window.popups[windowName].close();
}
window.popups[windowName] = window.open(url, windowName);
};
I would also listen for the unload event so you can remove your reference:
window.openOrFocus = function(url, windowName) {
if (!window.popups) {
window.popups = {};
}
if (window.popups[windowName]){
window.popups[windowName].close();
}
window.popups[windowName] = window.open(url, windowName);
window.popups[windowName].onunload = function() {
delete window.popups[windowName];
};
};
Side note: This is a syntax error:
window.openOrFocus = function(url, "abc") {
// --------------------------------^
I've replaced it with windowName in the code above.

Categories

Resources