Looked through the questions and there are few similar ones on the subject of "ReferenceError foo is not defined". However, I'm not able to detect the error in my code and get it working. It works fine in Chrome and Safari, but not in IE, Opera and Firefox:
The code in the HTML
<a href="javascript:foo(1)" target="_parent">
calls a javascript placed in the header as
<script type="text/javascript" src="http://www.site.com/include/script.js"></script>
which is defined as the following:
function foo(language){
url = window.parent.location.href;
parts = url.split('/');
page = parts[3];
newUrl = "";
if (language == 1){
newUrl = "http://www.site1.com/" + page;
} else if (language == 2){
newUrl = "http://www.site2.com/" + page;
} else{
newUrl = "http://www.site3.com/" + page;
}
window.parent.window.location.href = newUrl;
}
Reading the related questions I tested to change to window.foo = function(language){...}, but it didn't help.
Seems straight forward and as simple as it gets, but of some reason foo is undefined in IE and firefox.
Should be added that the javascript is in the "top.html" which is an embeded iframe for each page. Somehow chrome manages this while IE doesn't (but the script works if I browse to http://www.site1.com/top.html and click on the button calling redirect(language);)
Your problem is that the link is targeted (has a target="_parent" bit).
This means that it runs in the scope of the target window, not in the window it's in. And there is no function named foo there.
It look like your link is in a "iframe" tag, but the foo function is defined in top-level window object's scope.
There a two ways to fix this:
You should use window.partent to reference the top-level window object, try to change the link to
<a href="javascript:window.partent.foo(1)" target="_parent">
Or, move the function code to the same html file's head tag as the link.
By the way, you should use var keyword to declare variables.
Related
I'm trying to redirect to my price section of my webiste by the following link: https://www.paydomestic.com.br/#pricing
Google chrome works correctly, already in firefox does not work.
but this only occurs via link, if you put the url "https://www.paydomestic.com.br/#pricing" and press enter in the browser works, but not via link!
why is that?
Solved
<script>
/*location.hash returns the anchor part of an URL as a string,
with hash (#) symbol included. */
//wait for page elements to load
//execute function only if the anchor exists in the URL address
window.onload = function() {if(location.hash){
//remove # from the string
var elId = location.hash.replace('#','');
//locate the anchored element on the page by its ID property
var scrollToEl = document.getElementById(elId);
//scroll to the anchored element
scrollToEl.scrollIntoView(true);
}
}
</script>
used jssor slider , i have some pages with same jssor slider , some pages are working fine , but some pages comes Outer container 'null' not found. bug , can any one help on this ?
I had a similar problem, so did some digging to see what the issue was.
The setup starts with the initial call, here's the snippet from the demo site
http://www.jssor.com/development/index.html
var jssor_slider1 = new $JssorSlider$("slider1_container", options);
which, among setting up all kinds of utility functions- more importantly does this
function JssorSlider(elmt, options) {
var _SelfSlider = this;
...
// bunch of other functions
...
$JssorDebug$.$Execute(function () {
var outerContainerElmt = $Jssor$.$GetElement(elmt);
if (!outerContainerElmt)
$JssorDebug$.$Fail("Outer container '" + elmt + "' not found.");
});
}
so at this point, it's trying to collect the string you passed, which is the elmt variable- which is for what? Well let's take a look at that $GetElement function in jssor.js
_This.$GetElement = function (elmt) {
if (_This.$IsString(elmt)) {
elmt = document.getElementById(elmt);
}
return elmt;
};
So, really, what it comes down to is this line for finding the element.
elmt = document.getElementById(elmt);
So the base of this error is
"We tried to use your string to find a matching ID tag on the page and it didn't give us a valid value"
This could be a typo, or another line of code modifying/removing the DOM.
Note that there are some scripts try to remove or modify element in your page.
Please right click on your page and click 'Inspect Element' menu item in the context menu.
Check if the 'outer container' is still there in the document. And check if there is another element with the same id.
Check if "Slider1_Container" is present or Used.
In my case, I didn't have it in my html, but still I had added the js.
Removing js resolved my issue.
(Question1, question2 and question3 looks how to force users open link in new tab)
But in my situation I visit some sites regularly and they have links like this:
<a href='javascript:window.open("/view.php?id=1234","_self")'>Link name</a>
This type of link makes me impossible to open link in new tab with a mouse click. Every time I see these links, I duplicate the tab in Chrome and click link inside the cloned tab. And go back to original tab and continue to surf. Is it possible to open these links in new tab with a chrome extension, js code or something?
You can try one of the links here: http://bit.ly/12dUk4V
. . The problem is that these links can be kind of "about:blank" because they are not specified in the href attribute normally, so it breaks your expected behavior when using ctrl+click, middle click or something alike. Sometimes sites links to "javascript:" pseudo-protocol, sometimes the link is for "#" with a "onclick" trigger... It depends on the situation.
. . For this specific case it's easy enough to write a user script that will rewrite these kind of links, if you're willing to use something like Tampermonkey:
// ==UserScript==
// #name SelfLinks Fixer
// #namespace http://dnun.es./
// #version 0.1
// #description This script rewrites "window.open(..., '_self')" links so that you can click them as you wish.
// #match http://libgen.info/*
// #copyright 2013, http://dnun.es.
// ==/UserScript==
var tRegExp = '^javascript: *'+
'(window\\.)?open\\('+
' *(([\'"])([^\\3]+)\\3) *,'+
' *[\'"]_self[\'"] *'+
'\\) *;? *$';
var fixLinksCheck = new RegExp(tRegExp);
var as = document.getElementsByTagName('a'), i = 0, n = as.length, a;
for (;i<n;i++) { a = as[i];
if (fixLinksCheck.test(a.href)) { //damn you _self link!
a.href = a.href.replace(fixLinksCheck, '$4');
}
}
. . This code "fixes" only the "_self" links by changing them to normal links. You can then click them with middle button, holding ctrl/shift or whatever. It also leave the "_blank" or "_top" links untouched.
Yes, it is possible. All you need is to inject a simple line of JavaScript code in every page. I had done it before in a Firefox extension.
You just need to override window.open method:
var open_= window.open;
window.open = function(url, name, opts) {
if (name === '_self') { name = '_blank'; }
open_(url, '_blank', opts);
};
Complete code on JsFiddle: http://jsfiddle.net/dp4Uz/
I've created a page with an empty iframe on it. I can then select the iframe document and navigate to it's body:
var iframe = document.getElementsByTagName('iframe')[0];
var doc = iframe.contentDocument || iframe.contentWindow.document;
var body = doc.body;
console.log("Body is", body);
In firefox and chrome this gives me the body object. In IE10 it gives me null.
Here is a Jsbin demonstrating the issue. Open up the JS, Console, Output panels and click "Run With JS".
Two questions:
How do I get access to the iframe's body in a cross-browser manner?
Which is the correct "to-spec" behavior?
I had a similar problem earlier today. It seems IE, at least 9 and 10, doesn't create the iframe body correctly (when I used the developer tools I was able to see a body tag inside the iframe, but like you wasn't able to call it), when there's no specified src. It gives you null cause it doesn't exist.
The answer, to whether there is a cross browser manner to access the iframe's body, is no. BUT, you could use a workaround. First, check if the iframe body exist, if not, then create it.
Your code would look like this:
var iframe = document.getElementsByTagName('iframe')[0];
var doc = iframe.contentDocument || iframe.contentWindow.document;
// The workaround
if (doc.body == null) { // null in IE
doc.write("<body></body>");
}
var body = doc.body;
console.log("Body is", body);
Source: http://forums.asp.net/t/1686774.aspx/1
This code is working for me cross-browser:
var doc=ifr.contentWindow||ifr.contentDocument;
if (doc.document) doc=doc.document;
var body=doc.getElementByTagName("body")[0];
Over a year later but I believe the solution was to call
doc.open()
//make any modifications
doc.close()
//at this point doc.body will not be null
This made things work in a fairly consistent manner cross browser
In the body I have the following:
<body onload="loadCheck1();">
Which points to the following function:
function loadCheck1() {
var chkme1 = "<?php echo $_GET['update']; ?>";
if (chkme1 ==1){
window.location = "viewrecipe.php?recipe_name="+recipe_name.value+"&update=2&texty1="+texty1.value+"&texty2="+texty2.value+"&texty3="+texty3.value+"&texty4="+texty4.value+"&texty5="+texty5.value;
}
if (last==3){
window.location = "viewrecipe.php?recipe_name="+recipe_name.value+"&NewFG1="+texty4.value+"&NewAlc="+texty5.value;
}
}
The URL being viewrecipe.php?update=1 should load the first window.location and viewrecipe.php?update=3 should load the 2nd.
In Chrome this works absolutely fine. In IE I get an error saying "recipe_name" undefined.
The only thing I can think of is that in Chrome the code only activates after the actual loading of the page. As by that time all the field values will be filled in.
Is there an alternative method to running a function once the page has loaded?
There are several variables not defined in your function:
- recipe_name
- last
Are they defined somewhere else?
I guess that texty1 to texty5 are form inputs. If not these variables are not defined too.
You should give us more of your code so we can help you.