Get window top from <object> tag - javascript

Is it possible to fetch the window data of the "parent" window, from within a page loaded in a tag ?
I'm embedding a remote html file from https://test-static-app.herokuapp.com/ which simply outputs window.top and window.parent.top in the console.
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Test</title>
<script type="text/javascript">
console.log('window.top', window.top.location);
console.log('window.parent.top', window.parent.top.location);
</script>
</head>
<body>
</body>
</html>
The result is for both values:
{}
Is it possible to get this data? How ?

Related

No text is displaying in browser when execute html file

I have experience with Java and Python and am following along with this tutorial online (https://www.youtube.com/watch?v=W6NZfCO5SIk&t=579s) to learn Javascript. Despite copying and pasting his code (where I have a javascript file called "test.js" and a html file called "test.html", I am unable to have the "Hello world" text displaying in the browser page when it opens. (The title of the page displays correctly, but the "Hello world" text does not display in the page and there is only a blank page).
test.html:
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content = "ie=edge">
<title>Today's date</title>
</head>
<body>
<script src = "test.js">
</script>
</body>
test.js:
console.log("Hello world")
just press F12 to open your console
use alert display it in browser
oh i get you i guess
try this <h1></h1>
<body>
<h1>Hello World</h1>
<script src = "test.js">
</script>
</body>

change title in browser history after a page is loaded

This would show up as "a" in chrome://history/. I'm asking for a way to change the title in history when doing some AJAX after page is loaded.
a.html:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>a</title>
</head>
<body>
<script src="a.js" async></script>
</body>
</html>
a.js:
document.title="abv";

Link loads in new page, not iframe

OK, I've not used iframes for a long time but this problem is so basic I must be missing something really obvious.
I have a page (Page A) containing a link (to Page B in the same domain/folder as the parent) that I want to open in an iframe in Page A.
pageA.html:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Page A</title>
</head>
<body>
Load ...<br>
<iframe id="tgt"></iframe>
</body>
</html>
pageB.html:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Page B</title>
</head>
<body>
<p>This is a test. Should load in iframe.</p>
</body>
</html>
When the link is clicked pageB opens in a new page (as if the target id isn't present), not in the iframe in pageA.
What am I doing wrong?
=========================================================
OK, sorted that but now I need to do this using php generated JavaScript. Here is a very minimal example:
pageA.php (parent page):
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Page A</title>
<?php
$srcPage = "/pageB.html";
echo "
<script>
function loadIframe() {
window.alert('Clicked, loading $srcPage');
document.getElementById('tgt').attr('src', '$srcPage');
}
</script>
";?>
</head>
<body>
<p id="load" onclick="loadIframe()">[Click me to load] ...</p>
<iframe id="tgt" name="tgt"></iframe>
</body>
</html>
pageB.html (page to load - same as before):
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Page B</title>
</head>
<body>
<p>This is a test. Should load in iframe.</p>
</body>
</html>
The script executes (alert is displayed) but the page doesn't load.
Thanks
Nigel
You should add the attribute name="tgt" to your iframe.
open link in iframe
For your second question, you mixed jquery with plain javascript.
This is how you change the src of an iframe using plain javascript:
document.getElementById('iframeid').src = '$srcPage'

window.opener not working properly

I'm trying to use a simple scenario in which function from parent window is called from a popup.My source looks like this:
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Parent window</title>
</head>
<body>
<script type="text/javascript">
function twitterAccess() {
console.log('ok!')
}
window.open ("popup.html","mywindow", "width=350,height=250");
</script>
</body>
</html>
popup.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>popup</title>
</head>
<body>
<script type="text/javascript">
window.opener.twitterAccess()
</script>
</body>
</html>
But it allways gives me Uncaught TypeError: Object [object global] has no method 'twitterAccess'. I have also tried using it with window.parent, parent.window, parent.window.opener and a dozen of other suggestions found on the web but it's allways the same. Can you advice me on what am i doing wrong? I'm sure it's something really silly.

JavaScript won't work if placed inside the body of a web page?

I'm new to JavaScript and I've learned that JavaScript can be place between the <head></head> or <body></body> sections, I have been working in a project and it works fine inside the head but not the body section of the page.
examples:
working fine like this:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example Page</title>
<script>
function yetAnotherAlert(textToAlert) {
alert(textToAlert);
}
yetAnotherAlert("Hello World");
And is not working this way:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example Page</title>
</head>
<body>
<script>
function yetAnotherAlert(textToAlert) {
alert(textToAlert);
}
yetAnotherAlert("Hello World");
</script>
</body>
</html>
Your code snippet didn't show you closing the tag. Double check if you close your script block correctly.
It's also better to specify the type of scripting language too.
<script language='javascript'>
....
</script>

Categories

Resources