I have endlessly tried to open iframe links inside either the parent tab or a new tab however I just can't seem to get them to work. When I set the links to target="_parent" the links do nothing and when I set the links to target="_blank" they open in a new tab but also display nothing.
As soon as I get rid of the code that adds the target to the links they work but open inside the iframe which is not what I am trying to accomplish. Any help would be greatly appreciated.
Here is the iframe code that I am using:
<iframe src="js/eventsFrame2.php?EventID=2422308" id="frameclass" frameborder="0" scrolling="no" width="100%" height="600">
Here is the code inside the iframe:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
function myFunction() {
$("a").attr('target','_parent');
}
$(window).load(myFunction);
</script>
<script language="javascript" type="text/javascript">
document.write('<script language="javascript" src="http://tickettransaction.com/?bid=1761&sitenumber=18&tid=ticket_results&evtid=2422308"><\/script>');
</script>
If you have control over the content inside the iframe, you could try using the base tag.
Example -- In the page loaded into the iframe include a base tag within the head:
<base target="_top"/>
Have you tried overwriting the parent page (containing the iframe) with your content?
Code inside the iframe:
<script language="javascript" type="text/javascript">
parent.document.write('<script src="http://tickettransaction.com/?bid=1761&sitenumber=18&tid=ticket_results&evtid=2422308"><\/script>');
</script>
Query defines the contents() method to grab the document node, but it doesn't give you a cross-browser way to go from the document to the window, so you're still stuck with:
var iframea = $('iframe').contents().find("a");
iframea.attr('target','_parent');
IMPORTANT: you should always check that the document is with the status "complete" for work with this
var iframe= document.getElementById('iframe_id');
//var iframewindow= iframe.contentWindow
//? iframe.contentWindow : iframe.contentDocument.defaultView;
var preview = previewFrame.contentDocument || previewFrame.contentWindow.document;
if (preview.readyState=="complete")
{
//ok
}
else
{
//perform a recursive call until it is complete
}
Related
i try to fetch the frame head value ,but it doesn't work well,look at the following code
1.php
<iframe src="/admin/" ></iframe>
<script type="text/javascript">
// var a =
a = window.top.frames[0].a
alert(a)
</script>
/admin/index.php
<script type="text/javascript">
document.cookie = 'a=asdf';
a = 'asdf'
</script>
Look the result
when i open http://127.0.0.1/1.php in firefox or chrome it become that followings
open in browser
but when i execute it in the console it come into that
enter image description here
so My question is Why my code can't get the value of Tag iframe
You need to wrap your code under window load function or may be iframe load because iframe takes some time to load and you have to wait for it to be fully loadded.
<iframe src="/admin/" ></iframe>
<script type="text/javascript">
window.onload=function(){
// var a =
a = window.top.frames[0].a
alert(a)
}
</script>
I am currently using Ace (http://ace.c9.io/) to take code from an editor and execute it within an iFrame. My issue is that when I add something along the lines of
Code in Editor
<script type="text/javascript">
window.onload = function() {
alert("hello");
}
</script>
to the head or body of the iFrame, the code is never executed. This is my current code:
Injecting Code into iFrame
$("#btnRun").click(function(event) {
event.preventDefault();
// iFrame with id="preview"
var preview = $("#preview").contents();
// Get code from editor and wrap in <script> tags
var script = "<script type='text/javascript'>" + ace.edit("js-editor").getSession().getValue() + "</script>";
// Append code to head or body, in this case head
preview.find("head").append(script);
});
The code is successfully added to the iFrame, however it is never executed. I can also successfully add HTML/CSS and it displays in the iFrame, but the javascript is never touched.
I have tried wrapping the code in script tags within the editor only, as well as using an escape character on the closing tag: "</script>" but to no avail.
This is the iFrame in my index.html document.
iFrame in index.html
<iframe class="editor" id="preview" name="result" sandbox="allow-forms allow-popups allow-scripts allow-same-origin" frameborder="0">
#document
<!-- Editor content goes here -->
</iframe>
After the code is injected the iFrame looks like this
iFrame with Injected Code
<iframe class="editor" id="preview" name="result" sandbox="allow-forms allow-popups allow-scripts allow-same-origin" frameborder="0">
#document
<html>
<head>
<script type="text/javascript">
window.onload = function() {
alert("hello");
}
</script>
</head>
<body>
</body>
</html>
</iframe>
Also when calling the window.onload or window.top.onload event from within the iFrame, the code executes but only affects the page containing the iFrame and not the contents of the iFrame itself.
Thank you.
Note: When the code is not within window.onload it runs fine. However I wish to be able to execute this code when the frame's onload function is executed.
I would think that you are adding the JS to the iframe after the onload event has already fired.
Perhaps you could try simulating an event to run the preview code or dispatching the onload event again?
Might help: https://developer.mozilla.org/en-US/docs/Web/API/Window.dispatchEvent
I was able to fix this myself. The problem was that appending the script to be within the iFrame wasn't enough to make it work. To make the script only be executed within the iFrames DOM was to write directly to it.
$("#btnRun").click(function(event) {
event.preventDefault();
var previewDoc = window.frames[0].document;
var css = ace.edit("css-editor").getSession().getValue();
var script = ace.edit("js-editor").getSession().getValue();
var html = ace.edit("html-editor").getSession().getValue();
previewDoc.write("<!DOCTYPE html>");
previewDoc.write("<html>");
previewDoc.write("<head>");
previewDoc.write("<style type='text/css'>" + css + "</style>");
previewDoc.write("<script type='text/javascript'>window.onload = function() {" + script + "}</script>");
previewDoc.write("</head>");
previewDoc.write("<body>");
previewDoc.write(html);
previewDoc.write("</body>");
previewDoc.write("</html>");
previewDoc.close();
});
I think it would be more elegant to use the contentDocument property of the iframe, and then inject the script and trigger the parser so it actually interprets it as JavaScript.
I put up a small proof of concept on github. I hope it solves your problem in a more elegant manner.
https://github.com/opreaadrian/iframe-injected-scripts
Cheers,
Adrian.
I'm trying to create an external link, that appears within the same site.
Here, If I click on a link it should display the contents of that link(external site) within our site. i.e., if my site is siteA and in that I place a link (to siteB) that will redirect to 'siteB' closing 'siteA'. I just want to avoid the situation besides I want to make the siteB to be opened within the siteA
My idea is when the link is opened, an iframe will be created and the external site will be opened within that Iframe.
Google
<script type="text/javascript">
function executeOnClick(){
<iframe name="your_frame_name" src="www.google.com" > </iframe>
return true;
}
</script>
I wrote this code, but couldn't get what I expect. I tried to use button, but it's not working.
Is there any other way to fix my issue..
No javascript is needed, use target attribute:
Load the page
<iframe id="iframe1"> </iframe>
JSFiddle
IMPORTANT: you cannot display http://google.com in iframes because of their X-Frame-Options. See: The X-Frame-Options response header
function executeOnClick(){
$('body').append('<iframe name="your_frame_name" src="www.google.com" height=200 width=200 > </iframe>');
return true;
}
try this
The iframe markup should not be inside your JavaScript function. That does nothing except throwing an exception. Put the markup below your anchor and change its src on click:
Google
<iframe id="targetFrame"></iframe>
<script>
function executeOnClick(target) {
document.getElementById("targetFrame").src = target.src;
return false;
}
</script>
In fact you don't even need JavaScript to do this. You can use the target attribute of the anchor and set it to be the name of the frame you want to open the page in:
Google
<iframe name="targetFrame" id="targetFrame"></iframe>
Change to this:
onclick="executeOnClick(this); return false;"
and do this in your funcition:
<script type="text/javascript">
function executeOnClick(elem){
var frame = '<iframe name="your_frame_name" src="'+ elem.getAttribute('href') +'" > </iframe>';
document.body.appendChild(frame); // <----append it here.
}
</script>
In your onclick function you have to return false; to stop the default behaviour of an anchor to take you to the another page.
please note that the google.com will not work with iframe
Google
<iframe id="targetFrame"></iframe>
<script>
function executeOnClick(src) {
document.getElementById("targetFrame").src =src;
}
</script>
<iframe src="www.google.com" height="400" width="551" frameborder="0" scrolling="no" runat="server"></iframe>
Use runat attribute too. Hope this works.
If doesn't, what's the error?
I need to redirect page if div id found inside the page.
I have this script :
<script>
if (!document.getElementById("locationsHolder")) {
window.location.href = "logged.html";
}
</script>
On the same page I have this
<div class="locationsHolder" id="locationsHolder"></div>
Although I have everything right it loads logged.html page whatever id I put on getElementById. i.e. getElementById("notexist")
You should do if (document.getElementById("locationsHolder")) since you want to do something if the div is found. Also you should put the script at the end of document or use jQuery $(document).ready() to make sure the entire page is loaded prior to running the script.
Maybe this will do what you need:
<!-- Put the following somewhere in the head block: -->
<script type="text/javascript">
window.onload = function(){
if(document.getElementById("locationsHolder") == null){
// Do something here if the ID is NOT found on the page
}else{
// Do something here if the ID IS found on the page
document.location = "logged.html";
}
};
</script>
you probably tested for the existence of div id before even the page has been loaded.
I would suggest you to either place the script after the div id or invoke the call only after the onload event.
window.onload=function(){
// your code
}
you should ask if it is == null. try this in the developer tool.
!null --> true
!"" --> true
!document.getElementById("locationsHolder") is always true.
you should do something like
!document.getElementById("locationsHolder") == null
It was the '!' that I remove it and script was before div id. Now it's working.
I need it this because I want home page redirected if another page contain a div id. I have another script inside index file that looks for page2 if contain div id , and it worked until I made this script from this topic that checks if that div is present on the page.
So, this script is one home page and checks if div id found on page2 and loads it on home page :
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">
jQuery(function($){
$('#result').load('Page2.html #intro');
});
</script>
</head>
<body>
<div id="result"></div>
Code from page2:
<div id="result"><div id="intro">Hi, this is the Intro!</div></div>
Now the script from this topic can't find div id "intro" , and actually it dosen't load as bellow script search for 'intro'(and jquery above stops working), but if I search for other div from the page is working Must be a conflict.
<script>
if (document.getElementById("intro")) {
window.location.href = "logged.html";
}
</script>
What is wrong?
Maybe there is another more simple way to trigger redirection if page.2 contain div id "intro" ?
I tried to trigger redirect directly from page1 with this script but won't work:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"> </script>
<script type="text/javascript">
if ('page2.html #intro').length === 0){
window.location.href = "logged.html";
}
</script>
I though this would be simple enough but I can't find the answer. I need to know how I can access an iframe's name from within said iframe. I was trying something like this but it's not working.
<iframe name="thename">
<script type="text/javascript">
alert(parent.name);
</script>
</iframe>
There is a combination of answers I prefer:
// window.frameElement Gets IFrame element which document inside
window.frameElement.getAttribute("Name");
It works on IE7+, Mozilla & Chrome
You were nearly right. Setting the name attribute on a frame or iframe sets the name property of the frame's global window object to that string. (Not parent, which refers to the window of the document that owns the frame.)
So unless some other script has deliberately changed the name, it's as simple as:
1.html:
<iframe name="tim" href="2.html"></iframe>
2.html:
<script type="text/javascript">
alert(window.name); // tim
</script>
in some cases window.frameElement returns null in iframe, so i figured workaround for it.
1. you need to set hash in src url of iframe
<iframe name="tim" href="2.html#your-hash"></iframe>
2. in iframe you can get this hash with
<script type="text/javascript">
var hash = window.location.hash;
</script>
Well, an IFRAME element shouldn't contain anything, it's targeting another document. So using a SCRIPT tag inside an IFRAME doesn't make alot of sense. Instead, use the SCRIPT inside the called document, e.g.
iframe_caller.html:
<html>
<body>
<iframe id="theIframe" name="theIframe" src="iframe_doc.html"></iframe>
</body>
</html>
iframe_doc.html:
<html>
<body>
<script type="text/javascript">
var iframes= parent.document.getElementsByTagName("iframe");
document.write(iframes[0].getAttribute("id"));
</script>
</body>
</html>
Note I'm using parent.document.function() there.
Something like this should work:
parent.document.getElementById('idhere').name;
You have to use the parent and then get the element either byId, Name, etc... then access the name property.
So your code should be like:
<iframe name="thename">
<script type="text/javascript">
var iframeName = parent.document.getElementById('idhere').name;
</script>
</iframe>