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);
Related
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();
}
I currently have a button. When it's clicked I execute a javascript code.
var openAppBtn = document.getElementById("openAppBtn");
openAppBtn.onclick = function () {
var app = {
launchApp: function () {
window.location.replace("testappscheme://")
}
};
app.launchApp();
};
<a id="openAppBtn" class="btn" href="">Open KWCP App</a>
when I execute this bit of code on iOS, the page does a refresh if the app is not installed. May I ask how do I attempt to open the app without the page redirecting.
You could use an iframe to trigger the app intent. Example code for triggering the intent on the page-load for example. Just put it inside your click function.
<script type="text/javascript" charset="utf-8">
var frame = document.createElement('iframe');
frame.src = 'testappscheme:/' + window.location.pathname + window.location.search;
frame.style.display = 'none';
document.body.appendChild(frame);
// the following is optional, just to avoid an unnecessary iframe on the page
setTimeout(function() { document.body.removeChild(frame); }, 4);
</script>
Simply by clicking the button, add an iframe to your page with the schema-url.
$('a[href="/my-url"]').click(function(e) {
e.stopImmediatePropagation()
var win = window.open('https://newwebsite.com', '_blank');
});
I am trying to get a URL to stop opening up on SquareSpace and open a new link, but with the limitations I can only do so using javascript.
The code will open up the new window - but it will not stop the old url from also opening up in the current tab.
$('a[href="/my-url"]').click(function(e) {
e.preventDefault();
e.stopImmediatePropagation();
var win = window.open('https://newwebsite.com', '_blank');
});
Use event.preventDefault instead:
$('a[href="/my-url"]').click(e => {
e.preventDefault();
var win = window.open('https://newwebsite.com', '_blank');
});
I'm trying to open the href onMouseOver after 3000ms. But it just popups a blank window. What am I missing?
HTML:
My Rec
JavaScript:
var Popup = null;
function openwindow()
{
var win = window.open()
}
(OK, first off, you need to supply a URL to window.open(), otherwise it doesn't know what page to open to. Aside from that:)
When you do a setTimeout() the value of this is reset in the delayed code.
A quick fix is to extract the URL immediately, and then pass a function into setTimeout() that can use the variable.
<a href="../cc2b/myrec.html"
onMouseOver="var popupUrl = this.href; Popup = setTimeout(function(){openwindow(popupUrl)}), 3000);"
onMouseOut="clearInterval(Popup)">
My Rec
</a>
However, a cleaner solution would be to minimise the code in onMouseOver by setting a timeout in the openhoverpopup function:
<a href="../cc2b/myrec.html"
onMouseOver="openhoverpopup(this.href)"
onMouseOut="clearhoverpopup()">
My Rec
</a>
<script>
var popupTimeout = null;
function openhoverpopup(url) {
popupTimeout = setTimeout(function () {
window.open(url);
}, 3000);
}
function clearhoverpopup() {
clearTimeout(popupTimeout);
}
</script>
You can grab the URL from the element that triggered the mouseover event with either event.target or event.srcElement for older IE browsers.
http://jsfiddle.net/b42pr/1
HTML
Hover
JavaScript
function popURL() {
var url = event.target.href || event.srcElement.href;
console.log("Open URL: " + url);
setTimeout(function(){
window.open(url);
}, 3000)
}
this.href is undefined, I think you're looking for window.location.href:
> this.href
undefined
> window.location.href
"http://stackoverflow.com/questions/18981172/settimeout-window-open-cant-take-this-href"
Also, your function
function openwindow()
{
var win = window.open()
}
Takes no parameters and opens nothing. Change it to
function openwindow(target)
{
var win = window.open(target)
}
Be careful though, most pop-up blockers will block this kind of window.
Try specifying the href outside of the string:
My Rec
My site is AJAX based so I have an window.onbeforeunload = function(){};. However, I have mailto link and when I click those mailto links, the confirm reload pops up, which I don't like. Is there a way to write a function to remove the window.onbeforeunload = function(){};, pull up the mail editor from the mailto link, and then put the window.onbeforeunload = function(){}; back?
Thanks
You could add a script like this after your anchor:
Mail
<script>
(function(w, d){
var anchors = d.getElementsByTagName('a');
var a = anchors[anchors.length - 1];
a.onclick = function(){
var old_unload = w.onbeforeunload;
w.onbeforeunload = null;
w.location = a.href;
setTimeout(function(){
w.onbeforeunload = old_unload;
}, 0);
return false;
};
})(window, document);
</script>
JSFiddle Demo
Note: The first revision did not work, because w.onbeforeunload = old_unload was running before the browser changed to the mailto page. Using setTimeout to make the assignment asynchronous solves that problem.