My code below opens a random link and instead of opening the link in a new page I want it to open in an Iframe is this possible.
<button onclick="randomLink()" target="iframe_a";>Click here to go somewhere else!</button>
var randomLink = function () {
var links = [
"bing.com",
"google.com"
];
var max = (links.length)
var randomNumber = Math.floor(Math.random()*max);
var link = links[randomNumber];
window.location = "http://" + link ;
}
</script>
<iframe src="demo_iframe.htm" name="iframe_a"></iframe>
Simply change the src attribute of the iframe, replacing the window.location = "http://" + link ; line to the following:
document.getElementById('iframe_a').setAttribute('src', "https://" + link);
Or if you want to use jQuery:
$('iframe').attr('src', "https://" + link);
Just like in the #DinoMyte comment.
The javascript code would look like this:
var randomLink = function () {
var links = [
"bing.com",
"google.com"
];
var max = (links.length);
var randomNumber = Math.floor(Math.random()*max);
var link = links[randomNumber];
document.getElementById('iframe_a').setAttribute('src', "https://" + link);
};
The complete example is here: https://jsbin.com/cuzehu/1/edit?html,js,output
Related
I have a page ex: test123.com/go#1 on that page if the hash changes ex: test123.com/go#2 I'd like to append the new hash value to a target URL and link out to it: test123.com/gopage2#2
Here's the code I've tried so far:
var url = window.location.href;
var res = url.split("#");
var hash = "#" + res[1];
$(window).on('hashchange', function() {
window.location = 'test123.com/gopage2' + hash;
});
When the hash changes nothing happens... Any ideas?
Assuming you meant to update url, res, and hash in the event handler, and use the hash in the new URL, try this:
$(window).on('hashchange', function() {
var url = window.location.href;
var res = url.split("#");
var hash = "#" + res[1];
window.location = 'test123.com/gopage' + res[1] + hash;
});
I called this method on keyUp event of enter key
function PerformSearch(search_key) {
var url = "<%=ResolveUrl("/Test/TestPage.aspx?type=search&search_key=")%>" + search_key;
//GoToLocation(url);
//window.open(url);
//$(location).attr("href", url);
//window.location.replace(url);
window.location.href = url;
}
use like this
window.location = url;
For your reference read this
You have string concatenation errors in your url.
This:
var url = "<%=ResolveUrl("/Test/TestPage.aspx?type=search&search_key=")%>" + search_key;
Should be this:
var url = "<%=ResolveUrl('/Test/TestPage.aspx?type=search&search_key=')%>" + search_key;
i have soure code :
$("#yes-modal").click(function (e) {
var img = $("#url").val();
window.location = '#Url.Action("DownloadImages", "Hrd", new { id = *value variable img* })';
});
i want get value img, but i have error.
How can do it ? thank you.
Below syntax should work.
window.location = '#Url.Action("DownloadImages", "Hrd", new { id = ' + img + '})';
Let me know if you face any issue.
The razor syntax is a serverside code. It is evaluated while rendering the page from server. But the javascript is evaluated at the client side (browser). So you have to split it like below code.
$("#yes-modal").click(function (e) {
var img = $("#url").val();
var loc = '#Url.Action("DownloadImages", "Hrd")';
var loc += '?id=' + img;
window.location = loc;
});
My Url looks like: http://google.com/stackoverflow/post/1
Now, I want to get just part of URL: http://google.com/stackoverflow/ and add it to code:
Post . How to do it ? Thanks !
Plz try this:
var url = "http://google.com/stackoverflow/post/1";
var partOfUrl = url.split('post')[0];
Thanks.
UPDATED:
Using JavaScript:
var url = "http://google.com/stackoverflow/post/1"; // or var url = window.location;
var matches = url.match(/^http\:\/\/([\w\.]+)\/(\w+)\/.+/);
if( matches ){
var newURL = "http://" + matches[1] + "/" + matches[2] + "/";
alert( newURL );
}
document.getElementById('post_link').href = newURL;
HTML:
<a id="post_link">post</a>
See JSFiddle
use location Object
location.href
location.pathname
I need to send a mail using javascript but i was unable to do it with
can any one please help with the code.
var link == "mailto:test#test.com"
+ "&subject=" + escape(":Feedback");
window.location.h ref = link;
You assign link variable with == which is not correct. Try with:
var link = "mailto:RAS.AMB#Wilhelmsen.com&subject=" + escape(":Feedback");
window.location.href = link;
Your javascript is not valid. You have a space in href and var link == should be var link =
This works.
var link = "mailto:RAS.AMB#Wilhelmsen.com&subject=" + escape(":Feedback");
window.location.href = link;