Call JavaScript function within document.write() - javascript

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();
}

Related

Need to close all new window opened by my application

I have tried to close the new window by Window.close() this works for current tab close but I need to close all the new tabs opened by my application
var newwindow=null;
function NewWindow(mypage,myPagename)
{
params = 'width='+screen.width;
params += ', height='+screen.height;
params += ', top=0, left=0';
params +=', scrollbars=yes,menubar=yes,toolbar=yes';
newwindow=window.open(mypage,myPagename, params);
if (window.focus)
{
newwindow.focus()
}
return false;
}
Shall we close the new window by having their name , if yes means how ???
You can assign window.open() to a variable and then addEventListener to the window variable. Something like this:
var testWindow = window.open("enter/your/url");
testWindow.addEventListener('loadstart', yourListenerFunction);
Now when you want to close the window, you need to do this:
testWindow.removeEventListener('loadstart', yourListenerFunction);
testWindow.close();
removeEventListener() method removes the event listener previously registered with addEventListener().

Syntax issue at javascript, multiple functions in one file

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.

Redirect Current Tab From Firefox Extension

How can I redirect the current tab when a button in a panel is clicked?
What I currently have in my code right now is the following :
Javascript
function doSearch (){
alert("Search!");
var tabs = require("tabs");
tabs.open("http://google.com");
}
XUL
<xul:panel>
<xul:hbox>
<button onclick="doSearch ()">Button 1</button>
</xul:hbox>
</xul:panel>
Assuming by "redirect" you mean "navigate to"... how about this?
function doSearch (){
var win = window.top.getBrowser();
var tab = win.selectedBrowser;
tab.contentWindow.location.href = "http://google.com";
}
That should be OK provided your XUL is an overlay to the ChromeWindow hosting the TabBrowser in question.
See MDN: Tabbed Browser for some other scenarios.
i would recommend this:
function doSearch (e){
var win = e.target.ownerDocument.defaultView;
win.alert("Search!");
win.top.document.location.href = "http://google.com";
}
document.YOURBUTTON.addEventListener('click', doSearch, false);

Printing custom url with javascript not working

I'm trying to print another page on my domain by passing window.open the url, and using window.focus, window.print to print. However, the print preview shows only an empty page. I'm guessing I have to wait until the page is loaded, but I'm not sure exactly how to do this. Here's the code:
var newWin=window.open('http://mydomain.com');
newWin.focus();
newWin.print();
newWin.close();
I've tried stuff like
newWin.onload() {
newWin.print();
});
to no avail.
Edit 1:
var newWin=window.open('http://localhost:76');
newWin.focus();
newWin.onload = newWin.print();
newWin.close();
Same problem persists
Edit 2:
var newWin=window.open('http://localhost:76');
newWin.focus();
newWin.body.onload = newWin.print();
Adding newWin.close() here causes the print function to
bug out and only print the
title of the page. Otherwise, the page is printing properly with this
Edit 3:
function printWin(newWin) {
newWin.print();
newWin.close();
}
var newWin = window.open('http://localhost:76');
newWin.focus();
newWin.body.onload = printWin(newWin);
This causes the print to happen prematurely like before, previewing an empty page. wtf :(
Open the popup. Then check if the popup is ready. When it's ready, inject a javascript.
<script type="text/javascript">
var popup = window.open("b.htm");
var body;
function check() {
body = popup.document.getElementsByTagName("body");
if (body[0] == null) {
setTimeout(check, 50);
} else {
var n = popup.document.createElement("script");
n.src = "printandclose.js";
body.appendChild(n);
}
}
check();
</script>
printandclose.js
window.print();
window.open("", "_self");
window.close();
Let me know if it works. If you don't add window.open("", "_self"); a alert will popup telling the user that the window is about to close.

javascript open parent window

The following is in the NewForm.aspx which gets executed from a parent page (surveys/lists/testsurvey/allitems.aspx). The javascript works and open google. However, it opens the google in the pop up window (newform.aspx) but i need to close the popup window and show google.com on the parent window (or whatever the parent window link is)
<script type="text/javascript">
function redirect()
{
var inputcCtrls = document.getElementsByTagName("input");
for(m=0; m<inputcCtrls.length; m++)
{
if(inputcCtrls[m].type == 'button' && inputcCtrls[m].value == 'Finish')
{
var funcOnClick = inputcCtrls[m].onclick;
inputcCtrls[m].onclick = function () { window.location = "http://www.google.com/" }; }
}
}
redirect();
</script>
Use the opener property to find the window that opened the current window:
window.opener.location = "http://www.google.com/";
window.close();

Categories

Resources