I dynamically create a link to a. 'mywebsite' is a variable. Everything works but the mywebsite page replaces that of the code. I want to open mywebsite in another page.
May I have a help to solve the problem ?
var mylink = document.createElement("a");
var textForButton = document.createTextNode("More details");
mylink.appendChild(textForButton);
mylink.setAttribute("href", mywebsite);
document.body.appendChild(mylink);
You should set the linke target as this:
mylink.setAttribute("target", '_blank');
Your code my be something like this
<script>
window.onload = function () {
let mywebsite = "http://www.google.com";
var mylink = document.createElement("a");
var textForButton = document.createTextNode("More details");
mylink.appendChild(textForButton);
mylink.setAttribute("href", mywebsite);
mylink.setAttribute("target", '_blank');
document.body.appendChild(mylink);
};
</script>
You can set target=_blank attribute to do this.
mylink.setAttribute("target", "_blank");
So that once you click on the link that you have generated it opens in new tab.
Related
I am wanting to link an iframe that is untitled, and I have gotten some code that lets me create an iframe, I just cannot figure out how to link the iframe. My code just lets me open the iframes, and I cannot figure out how I could modify my code to make it linkable, and I have seen this done before, but I was wondering if anyone could help me. (I do use it as a bookmarklet as well)
Javascript:
(function() {
var win = window.open();
var f = win.document.createElement("iframe");
window.focus();
var url = win.prompt("enter url", "https://");
if (!url) return;
win.document.body = win.document.createElement("body");
f.style.width = "100vw";
f.style.height = "100vh";
win.document.body.appendChild(f);
f.src = url
})()
I have three links which generate an image and then share the image on a page or download the image directly.
Unfortunately none of these links are working in facebook in-app browser. They seem to work great in all other browsers I have tried. Is there something I can do to the code that is called on click?
I am guessing the two social links are not working somehow because of the "window.open" command? Should I somehow make it append as a link to the button on click?
Any help or direction is appreciated.
$(document).ready(function() {
$('.facebook').click(function() {
var path = new String(window.location);
var resultPath = 'https://website.com/generator/share/?img=<?=$file?>';
var fbpopup = window.open("https://www.facebook.com/sharer/sharer.php?hashtag=%23hashtag&u=" + resultPath, "_blank", "width=600, height=400, scrollbars=no");
});
$('.twitter').click(function() {
var path = new String(window.location);
var resultPath = 'https://website.com/generator/share/?img=<?=$file?>';
var twShareLink = 'text=Check out my personalized image and create your own! %23hashtag &url=' + resultPath;
var twpopup = window.open("http://twitter.com/intent/tweet?" + twShareLink , "_blank", "width=600, height=400, scrollbars=no");
});
$('.download').click(function() {
var downloadLink = document.createElement("a");
downloadLink.href = '<?=$url?>';
downloadLink.download = 'image.jpg';
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
});
});
I am using the following javascript (provided by m59 in this previous answer) to hide the destination of a link which usually shows up at the bottom of the browser window when hovering over a link:
<div>
<a data-href="http://www.google.com/"> LINK </a>
<script type="text/javascript">
var anchors = document.querySelectorAll('a[data-href]');
for (var i=0; i<anchors.length; ++i) {
var anchor = anchors[i];
var href = anchor.getAttribute('data-href');
anchor.addEventListener('click', function() {
window.location = href;
});
}
</script>
</div>
This works perfectly fine, but I would like the link to open in a new tab as well. How do I have to change the script in order to do so?
I tried using window.open('href','_blank'); instead of window.location = href; which did not work.
Unless im missing something, the most obvious thing to do is add a target to that anchor:
<a data-href="http://www.google.com/" target="blank"> LINK </a>
This is how to do it (try it on your code, stackoverflow blocks new tabs and links)
var anchors = document.querySelectorAll('a[data-href]');
for (var i=0; i<anchors.length; ++i) {
var anchor = anchors[i];
var href = anchor.getAttribute('data-href');
anchor.addEventListener('click', function() {
window.open(href,'','');
});
}
<div>
<a data-href="http://www.google.com/"> LINK </a>
</div>
You can see here, that syntax of window.open is:
var window = window.open(url, windowName, [windowFeatures]);
So to open link in new tab you should use something like:
var myNewTab = window.open(url, '_blank');
It will not work if you window is opened as '_blank'
Did you try window.open(href)?
i.e. In your example, href is a string (there are single quotes around it) as opposed to a variable.
Why not just use an onclick event handler to redirect the user?
link
function onClick(e) {
e.preventDefault();
document.location.href='www.google.ca';
}
I am trying to open a new window from javascript but nothing is being inserted into the html:
var callScriptText = $('#callScriptText').html();
var url = '/Action/CallScript/?callScript=';
// Open the current call script in a new window
var openWindow = window.open(url, 'callScriptPopup', 'width = 500, height = 500');
$(openWindow).html(callScriptText);
Does anyone know why?
Here's an example to open a new window with content using jQuery
<script>
function nWin() {
var w = window.open();
var html = $("#toNewWindow").html();
$(w.document.body).html(html);
}
$(function() {
$("a#print").click(nWin);
});
</script>
<div id="toNewWindow">
<p>Your content here</p>
</div>
Open
EDIT:
For those who say that this code doesn't work, here's a jsfiddle to try it http://jsfiddle.net/8dXvt/
Try this:
var x=window.open();
x.document.open();
x.document.write('content');
x.document.close();
I find it works in Chrome and IE.
Building upon #Emre's answer.
With javascript, you can chain, so I just modified the code to:
var x=window.open();
x.document.open().write('content');
x.close();
Also, to force it to a new window (not a new tab), give the first line dimensions. But it has to be the third argument. So change the first line to:
var x=window.open('','','width=600, height=600');
try:
var x = window.open('', '', 'location=no,toolbar=0');
x.document.body.innerHTML = 'content';
var codeContents = $("#contentsfornewWindow").html()
var win = window.open('', 'title', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=1000,height=1000,left=200,top=70');
win.document.body.innerHTML = codeContents;
VS:
win.document.write(codeContents);
I have notice if there are iframes like youtubes videos , win.document.write loads the iframes where as document.body.innerHTML does not!
I need help of getting this piece of code to work
1) I want to automatically start a media player when a pop-up is open, by changing is URL params, that the pop-up has
2) When the user clicks on on the link I want to replace the old URL with the one the new one clicked. Try using location.replace just ends up being a constant loop.
Not having much luck with the code can anyone help????
var vidlink = $('.link').attr('href');
var autoplay = $('.link:first').attr('href');
var myurl = window.location.href,
paramsStr = autoplay,
paramsObj = {};
var newUrl = $.param.querystring(myurl, paramsStr );
if(window.location.href != newUrl){
location.replace(newUrl);
}
$('.link').click(function(){
loadPlayer(vidlink);
});
Open Player
player another show <a href='?v=53&a=false' class='link'>Ep2</a>
try windowname.document.location = newURL (where windoname is the name of the pop-up)