Access child window elements from parent window using Javascript - javascript

I need to access child window elements from parent window. I have written the sample snippets below.
Parent HTML:
<html>
<head>
<title>Parent</title>
<style>
div{
float:left;
cursor:pointer;
}
</style>
<script type="text/javascript">
var SubpopUpWin="";
function Opennew(passedURL){
SubpopUpWin = window.open("popups.html", '_blank','toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes'); SubpopUpWin.document.getElementById("ifrm").src=passedURL;
SubpopUpWin.document.getElementById("ifrm_title").innerHTML=passedURL;
}
</script>
</head>
<body>
<div onclick="Opennew('http://www.google.com')">Google</div>
<div onclick="Opennew('http://www.yahoo.com')">Yahoo</div>
<div onclick="Opennew('http://www.bing.com')">Bing</div>
</body>
</html>
popups.html
<html>
<head>
<title>Child</title>
<style>
div{
float:left;
}
</style>
</head>
<body>
<div>
<div id="ifrm_title"></div>
<div style="margin-top:20px">
<iframe id="ifrm" src="" width="470" height="270" frameborder="0" style="margin-top: 34px" scrolling="no"></iframe>
</div>
</div>
</body>
</html>
In the above code is not working. Even I have used the below script also.
<script type="text/javascript">
var SubpopUpWin="";
function Opennew(passedURL){
SubpopUpWin = window.open("popups.html", '_blank','toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes');
SubpopUpWin.onload=function(){
SubpopUpWin.document.getElementById("ifrm").src=passedURL;
SubpopUpWin.document.getElementById("ifrm_title").innerHTML=passedURL;
}
}
</script>
The above code also not working.
Please share your sugestions/solutions...
Thanks

I feel this is because of some security. You can try this way instead:
<html>
<head>
<title>Parent</title>
<style>
div {
float: left;
cursor: pointer;
}
</style>
<script type="text/javascript">
var SubpopUpWin = "";
var testUrl = "";
function Opennew(passedURL){
testUrl = passedURL;
SubpopUpWin = window.open("popups.htm", '_blank', 'toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes');
}
</script>
</head>
<body>
<div onclick="Opennew('http://www.google.com')">
Google
</div>
<div onclick="Opennew('http://www.yahoo.com')">
Yahoo
</div>
<div onclick="Opennew('http://www.bing.com')">
Bing
</div>
</body>
<html>
<head>
<title>Child</title>
<style>
div {
float: left;
}
</style>
</head>
<body>
<div>
<div id="ifrm_title">
</div>
<div style="margin-top:20px">
<iframe id="ifrm" src="" width="470" height="270" frameborder="0" style="margin-top: 34px" scrolling="no">
</iframe>
<script type="text/javascript">
document.getElementById("ifrm").src = window.opener.testUrl;
</script>
</div>
</div>
</body>

Related

Trouble with Show/Hide code, second level

I cannot for the life of me figure out why the text under Sub1 and Sub2, do not show the text under them, when clicking them. Only the first link "Main Category" functions. It is making me enter more description, though the issue is already explained the best I know how.
<!DOCTYPE html>
<?php
session_start();
print "
<html>
</head>
<body>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
#body {
font-family: 'Helvetica', Arial, sans-serif;
display:none;
width: 100%;
padding: 5px 0;
text-align: left;
background-color: lightblue;
margin-top: 5px;}
</style>
</head>
<body>
<div id="body12">
Main Category</font>
<div class='showArticle'>
Sub1</font>
<div class='showComments'>
<font size="+1" color="red"><b>Text1</b></font>
</div><br>
Sub2</font>
<div class='showComments'>
<font size="+1" color="red"><b>Text2</b></font>
</div></div><br>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js' type='text/javascript'></script>
<script type='text/javascript'>
$(document).ready(function(){
$('.showArticle').hide();
$('.articleTitle').show();
$('.showComments').hide();
$('.commentTitle').show();
$('.articleTitle').click(function(e){
$(this).next('.showArticle').slideToggle();
e.preventDefault();
});
$('.commentTitle').click(function(e){
$(this).next('.showComments').slideToggle();
e.preventDefault();
});
});
</script>
</body>
</html>
You have a typo in your JS $('.commentsTitle').click(...
In the html you assigned the class commentsTitle, in the JS, you referred to commentTitle.
$(document).ready(function(){
$('.showArticle').hide();
$('.articleTitle').show();
$('.showComments').hide();
$('.commentsTitle').show();
$('.articleTitle').click(function(e){
$(this).next('.showArticle').slideToggle();
e.preventDefault();
});
$('.commentsTitle').click(function(e){
$(this).next('.showComments').slideToggle();
e.preventDefault();
});
});
<body>
<div id="body12">
Main Category</font>
<div class='showArticle'>
Sub1</font>
<div class='showComments'>
<font size="+1" color="red"><b>Text1</b></font>
</div><br>
Sub2</font>
<div class='showComments'>
<font size="+1" color="red"><b>Text2</b></font>
</div></div><br>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js' type='text/javascript'></script>
</body>
</html>

How To Add Prefix in Class's Name Using JavaScript

Hi All I am new in JavaScript. I have one HTML file. And I need to add some word in the class's name. Is there any way to do it using JavaScript?
For Example
<!Doctype HTML>
<html>
<head>
<style>
.heading{color:red;}
.text{color:red;}
</style>
</head>
<body>
<h1 class="heading">This is heading.</h1>
<p class="text">This is my text.</p>
</body>
</html>
In need to add "test-" in the class name in head and body both using JavaScript.
After using JavaScript function it should look like this.
<!Doctype HTML>
<html>
<head>
<style>
.test-heading{color:red;}
.test-text{color:red;}
</style>
</head>
<body>
<h1 class="test-heading">This is heading.</h1>
<p class="test-text">This is my text.</p>
</body>
</html>
It would be great help! Thanks in advance.
//$(".heading").addClass('test-heading').removeClass('heading');
//$(".text").addClass('test-text').removeClass('text')
$('.text').attr('class', function() {
return $(this).attr('class').replace('text', 'test-text');
});
$('.heading').attr('class', function() {
return $(this).attr('class').replace('heading', 'test-heading');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<html>
<head>
<style>
.heading{color:red;}
.text{color:red;}
</style>
</head>
<body>
<h1 class="heading">This is heading.</h1>
<p class="text">This is my text.</p>
</body>
</html>
You can loop Inside the body and add new class.
$("body").children().each(function(index, element) {
var oldclass = $(this).attr('class');
var newClass = "test_" + oldclass
$(this).removeClass(oldclass).addClass(newClass);
});
.test_heading {
color: blue;
}
.test_text {
color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<head>
<style>
.heading {
color: red;
}
.text {
color: red;
}
</style>
</head>
<body>
<h1 class="heading">This is heading.</h1>
<p class="text">This is my text.</p>
</body>
</html>
Find all classes of any element, loop through it, add new class and remove existing classes
var element = "body";
var className= "test-";
var classes = $(element).attr('class').split(" ");
classes.forEach(function(i,obj){
$(element).addClass(className + i);
$(element).removeClass(i);
});

Why the jQuery snippet works without the IFrame, but does not with an IFrame?

Works
$("a").on("click", function(e) {
alert(1);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
Link
</div>
Does not work
$("a").on("click", function(e) {
alert(1);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
Link
</div>
<iframe id="myIframe" src="" style="width: 200px; height: 200px;" />
Because your markup is invalid. iframe is not a void element. You should close the iframe properly.
The first rendered HTML in my Chrome browser:
<html><head>
<style>
</style>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
Link
</div>
<script type="text/javascript">
$("a").on("click", function(e) {
alert(1);
});
</script>
</body></html>
The second one:
<html><head>
<style>
</style>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
Link
</div>
<iframe id="myIframe" src="" style="width: 200px; height: 200px;">
<script type="text/javascript">
$("a").on("click", function(e) {
alert(1);
});
</script>
</body>
</html></iframe></body></html>

How to get div content in javascript variable?

I am trying to get the content of a div in a JavaScript variable.
I did try some code:
<html>
<head>
<script>
function data(){
alert();
var MyDiv1 = document.getElementById('newdata')
alert(MyDiv1);
}
</script>
</head>
<body>
<div id="newdata" style="background-color: red; width: 100px;height: 50px;">
1 <!-- The content I'm trying to get -->
</div>
Logout
</body>
</html>
But it does not work correctly.
Instead of
var MyDiv1 = document.getElementById('newdata')
alert(MyDiv1)
it should be
var MyDiv1 = document.getElementById('newdata').innerHTML
alert(MyDiv1)
OR
var MyDiv1 = document.getElementById('newdata')
alert(MyDiv1.innerHTML)
With .innerHTML you will get the html of specified element in the DOM.
EDIT:-
SEE DEMO HERE
You must use innerHTML.
<html>
<head>
</head>
<body>
<div id="newdata" style="background-color: red; width: 100px;height: 50px;">
1
</div>
Logout
</body>
<script>
function data() {
var MyDiv1 = document.getElementById('newdata').innerHTML;
alert(MyDiv1);
}
</script>
</html>

How to display user input in iframe

I have an input box for user to enter any url. Then I would like to display the website in a iframe after they click submit.
<html>
<head>
<title></title>
<style>
#netframe {
float:right;
height: 100%;
width: 80%;
}
#sidebar {
float:left;
height: 100%;
width: 20%;
}
</style>
</head>
<body>
<div id="container">
<div id="sidebar">
Enter A URL:
<input type="text" name="url" id="url">
<input type="button" value="load" id="load">
<br><br>
</div>
<div id="netframe">
<iframe height="100%" width="100%" class="netframe" src="pullsite.php" id="main_frame"></iframe>
</div>
</div>
</body>
</html>
This is the closest I found online but it does not do anything:
How To Reload A iframe Using jQuery
jsBin demo
HTML:
<input type="text" id="url" name="url" value="http://" />
<iframe height="90%" width="100%" src="" id="frame"></iframe>
jQuery
$('input#url').on('input', function() {
$('#frame').attr('src', this.value);
});
Try to add the following JS / jQuery script to your code:
<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>
So, your code will look like something like this:
<html>
<head>
<title></title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<style>
#netframe {
float:right;
height: 100%;
width: 80%;
}
#sidebar {
float:left;
height: 100%;
width: 20%;
}
</style>
<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="container">
<div id="sidebar">
Enter A URL:
<input type="text" name="url" id="url">
<input type="button" value="load" id="load">
<br><br>
</div>
<div id="netframe">
<iframe height="100%" width="100%" class="netframe" src="pullsite.php" id="main_frame"></iframe>
</div>
</div>
</body>
</html>
PS: Don't forget to load the jQuery library. You can load it from the web for example, by adding the following in your header:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
Hope this helps. Let me know if this works for you mate.

Categories

Resources