Make Sure that Object Element Has Page Fully loaded - javascript

I have an Object Element Which I Use To Load External Page On Click With a Button
My Html Is :
<input type="text" id="txt" />
<button onclick="getdata();return false;">Load page</button>
<div>
<object type="text/html" id="ExternalPageLoader" data="#">
</object>
</div>
And Script
<script>
function getdata() {
var url = $("#txt").val();
$("#ExternalPageLoader").attr("data", url);
}
</script>
How Can I Make Sure That Requested Page Has Been Fully Loaded Into Object Element
i Would Like To Check The External Page Not the Current Page

use with onload .
function getdata() {
var url = $("#txt").val();
$("#ExternalPageLoader").attr("data", url);
console.log('loading...')
}
function loading(){
console.log('loaded sucess')
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="txt" />
<button onclick="getdata();return false;">Load page</button>
<div>
<object type="text/html" id="ExternalPageLoader" data="" onload="loading()">
</object>
</div>

Try to use .ready() event:
$("#ExternalPageLoader").ready(function() {
// Handler for .ready() called.
});

Related

How do I force any iframe content to appear on top if changed, without changing the attributes in the links?

How do I force any iframe content to appear on top if the content is changed as if the link had the target="_top" attribute...
EXAMPLE 1: Normally this code is what would be applied in a page.
<script src="http://code.jquery.com/jquery-1.8.1.min.js"></script>
<script type="text/javascript">
if(top!=self){
top.location.replace(document.location);
alert("For security reasons, framing is not allowed; click OK to remove the frames.")
}
</script>
But I need to aply it to the iframe instead, and fire it only if the iframe is changed. How do I do that?
For example, I click on a link within the iframe, once that page changes URL then the iframe puts the page on target="_top" without having attributes for it in the iframe URL page, but the attributes for it should be from the iframe page itself.
EXAMPLE 2:
I have found a code that works as it should, the problem is only that it only fires when clicked on a button. Can I somehow make it to fire automatically when content of the iframe changes?
JavaScript:
<script type="text/javascript">
//<![CDATA[
$(window).load(function(){
document.getElementById('example-button').addEventListener('click', function (ev) {
ev.preventDefault();
var win = open('about:blank');
var iframe = document.getElementById('example-iframe');
setTimeout(function () {
var body = win.document.body;
body.style.padding = 0;
body.style.margin = 0;
body.appendChild(iframe);
iframe.style.position = 'fixed';
iframe.style.padding = 0;
iframe.style.margin = 0;
iframe.style.width = '100%';
iframe.style.height = '100%';
iframe.style.border = 0;
}, 100);
win.addEventListener('beforeunload', function () {
document.getElementById('example-container').appendChild(iframe);
iframe.style.position = '';
iframe.style.padding = '';
iframe.style.margin = '';
iframe.style.width = '';
iframe.style.height = '';
iframe.style.border = '';
});
});
});
//]]>
</script>
HTML:
<button id="example-button">open in new window</button>
<div id="example-container">
<iframe id="example-iframe" src="https://example.com/">
</iframe>
</div>
After fiddling and putting together with loads of ideas given online by others I came up with a solution that is 70% correct.
Now the only thing that remains is how to make it react ONLY when the iframe URL changes and not when loading like now.
JavaScript:
<script language="JavaScript">
function GoToURL()
{
var URLis;
URLis = document.URLframe.u.value
{
var location= (URLis);
window.open(location, '_blank');
}
}
</script>
HTML:
<form name="URLframe" id="URLframe" method="post">
<iframe id="test" src="https://example.com" onload="GoToURL(this);"></iframe>
<input type="hidden" name="u" size="71" value="" placeholder=" URL " id="SeekBox" onkeypress="handleKeyPress(event)">
<script type="text/javascript">
(function() {
document.getElementById('SeekBox').value = document.getElementById('test').src;
})();
</script>
</form>
To test it easier I used these codes at the bottom instead:
<iframe id="test" src="https://www.4shared.com/privacy.jsp" onload="loadurl();" width="100%" height="528px"></iframe>
<input type="text" name="u" size="71" value="" placeholder=" URL " id="SeekBox">
<input type="button" id="SeekButton" onclick="GoToURL(this);" value=" go ">
<input type="button" id="SeekButton" onclick="loadurl();" value=" load url ">
<input type="button" id="SeekButton" onclick="alerturl();" value=" alert url ">
<script type="text/javascript">
function alerturl() {
alert($('iframe').attr('src'));
}
</script>
<script type="text/javascript">
function loadurl() {
document.getElementById('SeekBox').value = document.getElementById('test').src;
}
</script>
If I understand you correctly then you want to get the src of the iframe any time there is a change within the iframe and then open a window with that src. If I'm right then it doesn't seem like it's possible. I tried to get the source of the iframe using this little bit of code but to no avail.
<iframe id="test" src="justanotherwebsite.com" onLoad="iframeLoad()"></iframe>
<script>
var counter = 0;
function iframeLoad(){
if (counter > 0){
console.log($('#test').attr('src'));
}
counter ++;
}
</script>
Let me know if I'm way off the mark on this and I'll have another look.

How to change iframe sources by order?

I want to change this code from changing the sources of iframe randomly to be by order
<script type="text/javascript">
function newSite() {
var sites = ['Experiment1/index.html',
'Experiment2/index.html',
'Experiment3/index.html']
document.getElementById('myIframe').src = sites[Math.floor(Math.random() * sites.length)];
}
</script>
when clicking on this button and also this is the iframe
<iframe id="myIframe" src="Experiment1/index.html" allowfullscreen="true" scrolling="no"></iframe><br/>
<input type="button" value="Go to next level" onClick="newSite()" />
Try defining sites array outside of function, using .shift()
<script type="text/javascript">
var sites = ['Experiment1/index.html',
'Experiment2/index.html',
'Experiment3/index.html'];
function newSite() {
var curr = sites.shift();
sites[sites.length] = curr;
document.getElementById('myIframe').src = curr;
}
</script>
<iframe id="myIframe"
src="Experiment1/index.html"
allowfullscreen="true"
scrolling="no"></iframe><br/>
<input type="button" value="Go to next level" onClick="newSite()" />
plnkr http://plnkr.co/edit/cQZznvyltX46UO1Y0lDG?p=preview

SoundCloud API - Any way of extracting the genre of a track loaded in to widget?

This code takes input of a SoundCloud track URL and loads it in to the widget. Is there anyway of being able to extract and display the genre/tags of this song using the SoundCloud API?
<html>
<head>
<script type='text/javascript' src='https://w.soundcloud.com/player/api.js'></script>
</head>
<body>
<div id="sectionLoad">
<input type="text" id="url" value=""><br>
<div id="gap"></div>
<a id="loadButton" onclick="loadSong();" class="button">LOAD TRACK</a>
<div id="gap2"></div>
<iframe id = "sc-widget" width="80%" height="80%" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=single_active=false" ></iframe>
</div>
<script>
function loadSong(){
(function() {
var iframe = document.querySelector('#sc-widget');
var widget = SC.Widget(iframe);
var input = document.getElementById("url");
widget.load(input.value);
console.log(widget);
}());
}
</script>
</body>
</html>
the Soundcloud widget has a getter method, getCurrentSound(), that'll return that information for you!
Notice you'll want to call getCurrentSound() only when the widget is done loading a song, otherwise it'll return null. SC already provides a ready event, SC.Widget.Events.READY, that you can bind that to, so after widget.load(input.value); add this bit:
widget.bind(SC.Widget.Events.READY, function(){
widget.getCurrentSound(function(currentSound) {
console.log(currentSound);
var genre = currentSound.genre;
var tags = currentSound.tag_list;
// add genre and tags to appropriate elements
});
});
I made a jsfiddle here where I broke the getCurrentSound call out into a separate function called showSongInfo: https://jsfiddle.net/dfnny3rp/

How to prevent links in iframe from opening in new tab

I made a little web-based web browser for a web-based OS I made. I noticed that in some sites, they have links that like to open in new tabs. Is there a way that this can be prevented and have the links open in the iframe instead?
Here's my code for the whole browser just in case:
<html>
<head>
<link href="./browser.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
$(function() {
$("#load").click(function() {
var new_url = $("#url").val();
// Checks that the user typed "http://" or not
if(new_url.substr(0,7)!="http://")
new_url = "http://"+new_url;
$("#main_frame").attr("src", new_url);
});
});
</script>
</head>
<body>
<div id="help">
<form action="help.html"><input type="submit" value="Help"></form>
</div>
Address:
<div id="logo">
<img src="stallion.png">
</div>
<input type="text" style="width: 400px;" name="url" id="url">
<input type="button" value="Go" id="load">
<div>
<input type="image" src="back.png" height=25 width=25 onclick="back()">
<input type="image" src="forward.png" height=25 width=25 onclick="forward()">
<input type="image" src="refresh.png" height=26 width=26 onclick="refresh()">
</div>
<iframe frameborder=0 class="netframe" src="http://www.bing.com/" id="main_frame"></iframe>
</body>
<script>
function back()
{
window.history.back();
}
</script>
<script>
function forward()
{
window.history.forward();
}
</script>
<script>
function refresh()
{
var iframe = document.getElementById('main_frame');
iframe.src = iframe.src;
}
</script>
</html>
There is two choices
1 : You can check all of the html in the iframe on each change and look for "target=_blank" and if so replace with "target=_self"
2 : Which I think would be the better way is, when the user clicks on an anchor tag check to see if the anchor has the attribute "target=_blank" if they do, simply remove it and then click the link.
I have provided a jsFiddle below
https://jsfiddle.net/L5dhp80e/
Html
<a class="newTab" href="http://www.google.com" target="_blank">
New Tab Google
</a>
<br />
<a class="removeBlank" href="http://www.google.com" target="_blank">
Removed Target Blank Google
</a>
Javascript
$(function () {
$('a.removeBlank').on('click', function () {
if ($(this).attr('target') == "_blank") {
$(this).attr('target', '_self');
}
$(this).click();
return false;
})
});
However if the iframe content is cross domain I don't think you can edit any of the code at all.
Get DOM content of cross-domain iframe
I found this answer in the web:
window.open = function (url, name, features, replace) {
document.getElementById('#iframe').src = url;
}
or with jQuery:
window.open = function (url, name, features, replace) {
$('#iframe').attr('src', url);
}
I haven't already tested it, but i hope it works.
Add target="_self"
<iframe src="http://google.com" target="_self"></iframe>
Target Attributes are:
_self = Opens the linked document in the same frame as it was clicked (this is default)
_blank = Opens the linked document in a new window or tab
_parent = Opens the linked document in the parent frame
_top = Opens the linked document in the full body of the window
framename = Opens the linked document in a named frame
Information from:
http://www.w3schools.com/tags/att_a_target.asp

replace all images with inputs elements jquery

i need find all images from textarea value and then replaces with inputs text elements
and the value of those with the src of the img was reemplace
after that the final string is output on a new div ('#newDiv')
my script dont replace nothing i dont know why
here is what i have done so far,
<html>
<head></head>
<body>
<textarea id="caja"></textarea>
<input type="button" onClick="parsingHtml()" value="read">
<div id="newDiv"></div>
<script type="text/javascript" src="jquery-1.11.1.min.js"></script>
function parsingHtml()
{
var content = $('#caja').val();
var newContent = $(content).find("img").replaceWith('<input type="text">');
alert(newContent)
$('#newDiv').html(newContent);
};
</body>
</html>
any ideas? thanks in advance!
There are 2 problems that I can see
newContent is referring to only the img elements, not the complete content
Since you are passing the value to jQuery, if the value does not start with < it will be considered as a selector not as a element creation command
So
//dom ready handler
jQuery(function($) {
//click event handler registration
$('#read').click(parsingHtml);
})
function parsingHtml() {
var content = $('#caja').val();
var newContent = $('<div />', {
html: content
});
newContent.find("img").replaceWith('<input type="text">');
console.log(newContent)
$('#newDiv').html(newContent.contents());
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<textarea id="caja"></textarea>
<input type="button" id="read" value="read">
<div id="newDiv"></div>

Categories

Resources