jquery - Opening Javascript onbutton Press/Link into an iframe -
my code below opens random link , instead of opening link in new page want open in iframe possible.
<button onclick="randomlink()" target="iframe_a";>click here 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 src attribute of iframe, replacing window.location = "http://" + link ; line following:
document.getelementbyid('iframe_a').setattribute('src', "https://" + link); or if want use jquery:
$('iframe').attr('src', "https://" + link); just in @dinomyte comment.
the javascript code 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 here: https://jsbin.com/cuzehu/1/edit?html,js,output
Comments
Post a Comment