pass a javascript variable from an iframe to the parent frame - javascript

So I have a variable in my iframe like so:
<script>
var zipphone = "<?= $phone ?>";
</script>
Now I want to pass that variable to the parent frame after the iframe is loaded. What is the simplest way to do that?

If the pages are both on the same domain, you could call a function of the parent window:
window.parent.zipPhoneCallback(zipphone);
In the parent window, you could define a function like this:
function zipPhoneCallback(zipphone) {
((console&&console.log)||alert)("zipphone = " + zipphone);
}

Beware there seems to be a Chrome browser issue with addressing variables from webpages to or from IFRAMES. This issue appears in offline testing.
That aside, assuming your browser actually implements basic functions of Javascript, you address your variable from your main webpage using window.myiframename.zipphone
<IFRAME NAME="myiframename" SRC="myzipphoneiframefile.htm" ....
<script type="text/javascript">
<!--
// then whatever you do with your variable
// read it
var z = window.myiframename.zipphone;
// write to it
window.myiframename.zipphone = "....";
and so on.
Example.
DOC1.HTM contents -
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>DOC1.HTM</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor=pink>
<h1>DOC1.HTM</h1>
<iframe name="iframename" src="doc2.htm"></iframe>
<p>
<br />
check variable
</p>
</body>
</html>
DOC2.HTM contents -
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>DOC2.HTM</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor=red>
<script type="text/javascript">
var test_var="testing, testing . . .";
</script>
<h1>DOC2.HTM</h1>
</body>
</html>
That works beautifully even with older versions of Internet Explorer but try it when offline testing with Chrome and window.iframename.test_var appears to be undefined because of the Chrome issue.
Anyway, look out for future versions of Chrome fixing this because it is a lot of egg on Google's face while they haven't.
I have a work-around for this issue in Chrome offline testing.
Thanks to Lucy24 on WebmasterWorld for helping. http://www.webmasterworld.com/google_chrome/4689258.htm#msg4689342
This issue with Chrome arose when my javascript was being tested off line and files doc1.htm and doc2.htm are in the same folder on my PC.
Moving the doc1.htm & doc2.htm files to a folder where I test my server side php programs, which runs using Windows Internet Services Manager means I can address the files using h t t p : / / l o c a l h o s t addresses and bingo, Chrome behaves as it should have behaved in offline mode.
It is not "legitimate" in my opinion for Chrome's javascript not to be able to directly address files in the same offline folder.
There's absolutely no security issue when you are running your own javascript files on your own PC. Or if Chrome wanted to offer a security setting that allowed or disallowed offline IFRAME variable scoping then OK, that would be fine.
The error message is not at all clear I submit and Lucy24 did well to figure out what it meant.
If Internet Explorer and Firefox etc allow you to test your javascript offline then why not Chrome?
So well done Lucy24. Not so well done Chrome.

Related

Run hide cmd.exe from website (using javascript or VBScript)

Why is the following example not working? Additionally, I would like to hide window of cmd.exe in createEmptyFile() function. The browsers should probably not block this code :
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Default Page Title</title>
</head>
<body>
<script type="text/javascript" language="javascript">
function pingItjs(ipAddress) {
var oShell = new ActiveXObject("wscript.shell");
oShell.Run("cmd.exe /k ping" + ipAddress);
}
function createEmptyFile() {
var oShell = new ActiveXObject("wscript.shell");
oShell.Run("cmd.exe /c cd %tmp% && echo hello > EmptyFile");
}
</script>
<script language="VBScript">
function pingIt(ipAddress)
set WshShell = CreateObject("WScript.Shell")
WshShell.Run("cmd.exe /k ping " & ipAddress)
end function
</script>
ping
<div onclick="call pingIt('216.58.215.78')">ping</div>
ping
</body>
</html>
ActiveX
The MDN reference states:
Note: Creating an ActiveXObject on a remote server is not supported in Internet Explorer 9 standards mode, Internet Explorer 10 standards mode, Internet Explorer 11 standards mode, and Windows Store apps or later.
So, unless the script is being run in your local machine, the code will not run. It appears that this can be stretched somewhat in the case of intranets, where the MDN says:
Important: ActiveX objects may present security issues. To use the ActiveXObject, you may need to adjust security settings in Internet Explorer for the relevant security zone. For example, for the local intranet zone, you typically need to change a custom setting to "Initialize and script ActiveX controls not marked as safe for scripting."
VBScript
VBScript is a proprietary Microsoft language and as such only works in IE. See this Answer. This link (from the Answer just referenced) states that VBScript has been deprecated in IE 11 and will not work in IE 11 in Edge mode.
Possibly, this may help you make VBScript work in IE 11 even in Edge compatibility mode,
<meta http-equiv="x-ua-compatible" content="IE=10">

javascript Open gmail and signin automaticly

I'm just building an experimental personal site where I could log myself into gmail with the press of a button.
Everithing goes fine, when I manually go to the gmail site and type the javascript command into the url line like this and press enter:
javascript: document.getElementsByName('Email').item(0).value='name'; document.getElementsByName('Passwd').item(0).value='password'; document.getElementsByName('signIn').item(0).click(); void(0);
But when I try to do the same from a page, nothing happens... Can someone please help me out?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Test</title>
<script>
var name = 'name';
function login(url1) {
win = window.open(url1, nome);
win.document.getElementsByName('Email').item(0).value='name';
win.document.getElementsByName('Passwd').item(0).value='password';
win.document.getElementsByName('signIn').item(0).click(); void(0);
}
</script>
</head>
<body>
<button id="buton1" onclick="login('https://accounts.google.com')">GMAIL LOGIN</button>
</body>
</html>
I've checked a lot of forums and answers, and it seemed that this method should work, but it doesn't.
Now imagine that your code was followed by similar code that would send lots of spam to people using your GMail account.
Cross domain DOM manipulation is forbidden by browsers as it would be a huge security hole.
The exception is postMessage, but that requires the cooperation of both sites.
You need a browser extension for this sort of thing.

How to get rid of the warning while rendering MathJax in IE

Although I found a similar Question
Using mark of the web with MathML,
I couldn't find the solution.
I tried the following sample from MathJax website
<!DOCTYPE html>
<html>
<head>
<title>MathJax TeX Test Page</title>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});
</script>
<script type="text/javascript"
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
</head>
<body>
<span style='font-size:130%'>
When $a \ne 0$, there are two solutions to \(ax^2 + bx + c = 0\) and they are
$$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$
</span>
</body>
</html>
I get
Internet Explorer restricted this webpage from running scripts or
ActiveX controls.
warning in IE 9.
This mathematical blog
http://swanlotus.com/varieties-of-multiplication/
works fine in IE 9.
Just wondering what changes do I need to make to the sample to fix this issue.
Problems like these exist because you're running the webpage directly from your computer, in other words you'll need to serve the page using a http server ( like apache, IIS, etc ).
You can still disable the warning, if you want to, by going to Tools (menu) → Internet Options → Security (tab) → Custom Level (button) → and Disable Automatic prompting for ActiveX controls. This will only apply to your computer obviously.
Anyway, if you're developing a webpage of sorts i recommend you run it in a http server in your localhost ( try installing WAMP for instance, gives you apache and php + mysql, and its easy to use )

magento javascript not playing nice with jquery

I'm having a bit of a problem getting my custom JavaScript file to play nice in Google chrome when jquery is being loaded in my Magento site. This problem only seems to be appearing in Google chrome. I've tested for the problem in both Linux Ubuntu 10.10 (chrome 10.0.648.114 beta) and Windows XP (chrome 9.0.597.98). FF, Safari, IE don't seem to have this problem.
When jQuery is added to the JS files to include, my personal JS file does not work when a hard refresh is sent (force a 200 request of the js file). if I do a regular refresh (304 request of the js file) and have it load from cache, then it works.
I have gone so far as to create a dummy page outside my magento site and I'm still getting this problem when I include the JS files from the magento site. but, if I move the JS files outside the magento site it works fine?
I have caching disabled in my magento admin and to my knowledge there isn't any module or custom code/plugins that would affect JS files that are to be included.
Here is an example of my dummy page that is sitting in the root directory of my server
The only thing gearlists.js has in it, is
alert('external');
so I would expect two alert popups when I load that page. but on hard refreshes, I'm only getting one popup. "Internal".
If i turn around and do a regular refresh, both JS files are loaded from the browser cache w/ a 304 status code and I get two popups "External" then "Internal"
Does Not Work With Hard Refresh
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://VIRTUAL_SERVER_FOR_MAGENTO_SITE/catalog/js/jquery/jquery-1.5.1.js"></script>
<script type="text/javascript" src="http://VIRTUAL_SERVER_FOR_MAGENTO_SITE/catalog/js/ads/gearlists.js"></script>
<script type="text/javascript">
alert('Internal');
</script>
</head>
<body>
</body>
</html>
Always Works
In this example, if I move the JS files out of the magento virtual server and into the root directory of my server, everything always works as expected. two popups are shown w/ every page refresh
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://localhost/jquery-1.5.1.js"></script>
<script type="text/javascript" src="http://localhost/gearlists.js"></script>
<script type="text/javascript">
alert('here');
</script>
</head>
<body>
</body>
</html>
Now if I remove the jquery file from my test, my custom js file always works fine whether its served from withing my magento VS or not...There is always two popups.
I'm a bit stuck on what could be causing this. Any ideas would be greatly appreciated.
OK, I figured this out. dumb mistake, but it took some time. The directory for the files were the initials for the site name js/ads/gearlists.js
turns out my adblock extension uses a regex to find the word "ads" and filtered my script out. when I adjusted the directory from ads to adsinc problem went away.
makes sense that id didn't work in linux or windows since my chrome extensions are synced.
Two tips:
You may want to install the mxperts jquery plugin extension on Magento Connect - it is free and helps debugging jquery scripts that people put on your Magento site.
Google Chrome has no known way of refreshing the cache - you can rename your js files though, then it loads new ones.

My processing.js sketch doesn't load. This shouldn't be too hard to answer

I wanted to run a test of a sketch I just made ported to processing.js. When I load the webpage, I get the following errors from the javascript console in Chrome:
XMLHttpRequest cannot load file:///Users/aoeuaoeu/Desktop/projects/local%20site%20files/_/ee.pde. Origin null is not allowed by Access-Control-Allow-Origin.
processing.js:27Uncaught Error: NETWORK_ERR: XMLHttpRequest Exception 101
Here's the source of the page I'm loading:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Earnest Emporium</title>
<!--Contact: earnestemporium#gmail.com-->
<script src="processing.js"></script>
<canvas data-processing-sources="ee.pde"></canvas>
</head>
<body>
</body>
</html>
ee.pde and processing.js are in the same directory as the html file, so I'm not really sure what's going on. I should also point out that I'm testing this locally, and I get a different error when I load the page once uploaded onto my ftp server. (Maybe it's chrome? I get a black box which doesn't show up on chrome on firefox once it's uploaded to the ftp server. It works fine on firefox locally)
/processing.js:17946Uncaught SyntaxError: Unexpected token <
Processing.Sketch.attach/processing.js:17946
Processing.Processing.executeSketch/processing.js:16209
Processing/processing.js:16237
init/processing.js:17991
window.addEventListener.i
I just noticed this:
file:///Users/aoeuaoeu/Desktop/projects/local%20site%20files/_/ee.pde.
Are you running this on your desktop? If I recall correctly AJAX does NOT run from your desktop, you need a native web server setup, and running it by calling your localhost.

Categories