Javascript Function Undefined but in Source - javascript

This music site shows the dreamweaver function MM_timelinePlay as undefined in chrome and firebug, but it is clearly defined in the header. The header is pulled in dynamically, but this should not matter as it has loaded by the time triggers it. The function is called on the hover state of a button labeled "more."

The code on line 62 is the following (within the function body for MM_initTimelines ):
px", "4px");
This is triggering a JavaScript error when the JS is being parsed, which seeing as the MM_timelinePlay is after the parse error, that function is not actually available to the page for use. Not sure what is supposed to be on line 62, but it appears that the line got munched somewhere in the process (perhaps as part of a bad copy and paste, or as part of at bad edit).
You may be able to recover the line by using the else branch of the if statement that line 62 is part of, here it is:
document.MM_Time[0][0].values[0] = new Array(-141,-131,-120,-110,-100,-89,-79,-69,-58,-48,-37,-27,-17,-6,4);
Change line 62 to something like the following:
document.MM_Time[0][0].values[0] = new Array("-141px","-131px","-120px","-110px","-100px","-89px","-79px","-69px","-58px","-48px","-37px","-27px","-17px","-6px","4px");
Make sure it's all on one line. The edits made were to simply wrap each value of the array with " and then include px. Based on searches for the MM_initTimelines function, the matching line of code (which differs based on the specific movements defined, the true branch of the if statement in your code should match the else except that the values are strings with the px unit added.
As timelines are no longer part of Dreamweaver (removed as of CS5 I think, but maybe earilier), you won't be able to restore or edit the timelines if you are using a newer version of Dreamweaver, so keep the old one around.

Related

Does this kind of value setting work in Javascript?

I'm trying to create code that requires the least number of bytes and that works for all browsers including IE 7.
In this example, the program calls dosomething('x1') and dosomething('x2').
If I have code like this:
var items,item,index,count;
items=Array('x1','x2');
count=items.length;
for (index=0;index<count;index++){
item=items[index];
dosomething(item);
}
Could I reduce it to this and have it still function exactly the same in all browsers:
var a=Array('x1','x2'),c=a.length,i;
for (i=0;i<c;i++){
f(a[i]);
}
I understand I changed the variable names and calling function name but my goal is to use the least number of bytes possible in the code to make the code execute.
I'm just not sure if declaring a variable equal to a property of a value from a previous variable in the same list of declarations would actually return correct results.
In other words, does var a=Array('x1','x2'),c=a.length... work, or do I have to specifically do var a=Array('x1','x2');var c=a.length; to make it work in all browsers including IE 7?
This is what the Google Closure Compiler service returned:
var a,b,c,d;a=["x1","x2"];d=a.length;for(c=0​;c<d;c++)b=a[c],dosomething(b);
You can find many different Javascript compressors online to automate the process you are hand coding now. Yet, it's always good to understand how they work as it helps to write code that is better compressed.
As for IE, you can test your code by changing the emulations settings in the IE debugger panel. Just press F12, click the Emulation tab, and adjust the document mode to 7 (IE7).
Hope this is enough to get you started in the right direction.
You can use Array.map from IE 9
var items = Array('x1','x2');
items.map(dosomething(item));

IE expected ')' error

I am receiving the following error in IE which is stopping the page from loading. I have tried to find this error but for the life of me cannot see it. The portion of the code that the error points to is here at jsfiddle.net:
http://jsfiddle.net/SrgsC/
I would be grateful if someone could point out the offending error? Many thanks
I am using php5.3.5
Message: Expected ')'
Line: 2867
Char: 41
Code: 0
URI: http://sample.com/admin/cp.php
Message: Expected ')'
Line: 2867
Char: 40
Code: 0
URI: http://sample.com/admin/cp.php
Well, the code you provided is in PHP, not JavaScript. If this is only happening in IE, then it is clear that you have put a PHP function in a JavaScript file. Otherwise, it should be happening in IE, FF, Chrome...
Some issues with the PHP function at any rate:
you start with $print .= . This means, "add the following string to the $print" variable, but $print is not defined in that function. If it is a global variable, you need to put global $print; on the line before it, or, if it is local, then you need to use $print = (no '.')
mysql_numrows should be mysql_num_rows unless you've created your own numrows function
mysql_result should not be used that way. To quote the docs:
When working on large result sets, you should consider using one of the functions that fetch an entire row
You should not be manually iterating through rows by tracking the current row number (the same reason as above)
A 15 parameter function is too many. It is way to many. The maximum which can be handled effectively are 7 (according to Code Complete), the more zealous would argue for even fewer (Uncle Bob, I believe, says 3 is a good standard). Either way, if I saw 8 or more parameters in a method during a code review I would immediately know something needed to change.

Whats happening? One day its OK, the next day its 'undefined'?

I am writing a greasemonkey script. Recently i had this same problem twice and i have no idea why is this happening.
function colli(){
.....
var oPriorityMass = bynID('massadderPriority');//my own document.getElementById() function
var aPriorities = [];
if (oPriorityMass) {
for (var cEntry=0; cEntry < oPriorityMass.childNodes.length; cEntry++) {
var sCollNumber = oPriorityMass.childNodes[cEntry].getAttribute('coll');
if (bynID('adder' + sCollNumber + '_check').checked)
aPriorities.push(parseInt(sCollNumber));
}
}
.....
}
So the mystery of this is, one day i had oPriorityMass named as oPririoty. It was working fine, but the whole function was not yet complete and i started working on another functions for my script. These functions have no connection with each other.
Few days later i decided to go back to my function in the above example and finish it. I ran a test on it without modifying anything and got an error in the firefox's (4) javascript error console saying that oPriority.chilNodes[cEntry] is undefined. NOTE, few days back i have tested it exactly the same way and there was no such problem at all.
Ok, so, i decided to rename oPriority to oPriorityMass. Magically, problem got solved.
At first i thought, maybe there was some conflict of 2 objects, with the same name being used in different functions, which somehow continued to live even outside of function scope. My script is currently over 6000 lines big, but i did a search and found out that oPriority was not mentioned anywhere else but in this exact function.
Can somebody tell me, how and why is this happening? I mentioned same thing happened twice now and they happened in different functions, but the same problem node.childNodes[c] is undefined yet node is not null and node.childNodes.length show correct child count.
What is going on? How do i avoid such problems?
Thank you
EDIT: The error given by error console is
Error: uncaught exception: TypeError: oPriorityMass.childNodes[cEntry] is undefined
In response to Brocks comment:
GM_log(oPriorityMass.childNodes[cEntry]) returns undefined as a message. So node.childNodes[c] is the thing that is undefined in general.
My script creates a div window. Later, the above function uses elements in this div. Elements do have unique IDs and i am 100% sure the original site don't know about them.
My script has a start/stop button to run one or the other function when i need to.
I have been refreshing the page and running my script function now. I have noticed that sometimes (but not always) script will fail with the described error on the first run, however, if i run it again (without refreshing the page) it starts working.
The page has a javascript that modifies it. It changes some of it's element widths so it changes when the browser is resized. But i know it has no effect on my div as it is left unchanged when i resize browser.
EDIT2:
function bynID(sID) {
return top.document.getElementById(ns(sID));
}
function ns(sText) {
return g_sScriptName + '_' + sText;
}
ns function just adds the script name in front of the ID. I use it when creating HTML element so my elements never have the same id as the web page. So bynID() is simple function that saves some typing time when i need to get element by ID.
I have modified my colli() function to include check
if (oPriorityMass) {
if (!oPriorityMass.childNodes[0]) {
GM_log('Retrying');
setTimeout(loadPage,2000);
return;
}
for (var cEntry=0; cEntry < oPriorityMass.childNodes.length; cEntry++) {
var sCollNumber = oPriorityMass.childNodes[cEntry].getAttribute('coll');
if (bynID('adder' + sCollNumber + '_check').checked)
aPriorities.push(parseInt(sCollNumber));
}
}
The loadPage function does 1 AJAX call, then i run few XPATH queries on it, but the actual contents are never appended/shown on the page, just kept inside document.createElement('div'), then this function calls colli(). So now, as i have modified my function, i checked the error console and saw that it may take up to 5 tries for it to start working correctly. 5 x 2seconds, thats 10 seconds. It is never 5 retries always, may vary There's got to be something else going on?
In Firefox, childNodes can include #text nodes. You should check to make sure that childNodes[cEntry] has nodeType == 1 or has a getAttribute method before trying to call it. e.g.
<div id="d0">
</div>
<div id="d1"></div>
In the above in Firefox and similar browsers (i.e. based on Gecko and WebKit based browsers like Safari), d0 has one child node, a text node, and d1 has no child nodes.
So I would do something like:
var sCollNumber, el0, el1;
if (oPriorityMass) {
for (var cEntry=0; cEntry < oPriorityMass.childNodes.length; cEntry++) {
el0 = oPriorityMass.childNodes[cEntry];
// Make sure have an HTMLElement that will
// have a getAttribute method
if (el0.nodeType == 1) {
sCollNumber = el0.getAttribute('coll');
el1 = bynID('adder' + sCollNumber + '_check');
// Make sure el1 is not falsey before attempting to
// access properties
if (el1 && el1.checked)
// Never call parseInt on strings without a radix
// Or use some other method to convert to Number
aPriorities.push(parseInt(sCollNumber, 10));
}
}
Given that sCollNumber seems like it is a string integer (just guessing but it seems likely), you can also use:
Number(sCollNumber)
or
+sCollNumber
whichever suits and is more maintainable.
So, according to your last edit, it now works, with the delay, right?
But when I suggested the delay it was not meant to do (even more?) ajax calls while waiting!!
NOT:
if (!oPriorityMass.childNodes[0]) {
GM_log('Retrying');
setTimeout(loadPage,2000);
return;
More like:
setTimeout (colli, 2000);
So the ajax and the other stuff that loadPage does could explain the excessive delay.
The random behavior could be caused by:
return top.document.getElementById(ns(sID));
This will cause erratic behavior if any frames or iframes are present, and you do not block operation on frames. (If you do block such operation then top is redundant and unnecessary.)
GM does not operate correctly in such cases -- depending on what the script does -- often seeming to "switch" from top scope to frame scope or vice versa.
So, it's probably best to change that to:
return document.getElementById (ns (sID) );
And make sure you have:
if (window.top != window.self) //-- Don't run on frames or iframes
return;
as the top lines of code.
Beyond that, it's near impossible to see the problem, because of insufficient information.
Either boil the problem into a Complete, Self Contained, Recipe for duplicating the failure.
OR, post or link to the Complete, Unedited, Script.

textmate reformat with 2 spaces

I've set textmate to use softtabs 2 spaces on my file. But when I try to reformat the entire document, it uses 2 hard tabs as the indents.
Regular indents work as I want it to, just the document format doesn't. Anyway to get textmate to be obedient?
Thanks.
The JavaScript bundle's "Reformat Document / Selection" command is passing the document's text to the js_beautify function in the bundle's beautify.php file (found on my system and probably by default at /Applications/TextMate.app/Contents/SharedSupport/Bundles/JavaScript.tmbundle/Support/lib/beautify.php). If you take a look at the function definition you'll see that there's a second parameter, $tab_size, with a default value of 4. There's a line in the bundle that reads print js_beautify($input);. Change this to print js_beautify($input, 2); and you should, I expect, get tab stops with two spaces.
To make it a bit more flexible, use the TextMate environment variable TM_TAB_SIZE, as in print js_beautify( $input, getenv('TM_TAB_SIZE' ) );, which should update how the command operates if you ever change your tab size.
Note, I've tested none of this. :) Just took a look at the bundle and tracked down what seems to be necessary.
So, I tried chuck's suggestion and it gave me an error. I did this to "fix it". I'm sure it could be done more elegantly, but this worked for me.
Open up the same file Chuck says to open up, line 50 (or so) should look like this:
function js_beautify($js_source_text, $tab_size = 4)
change $tab_size to 1
function js_beautify($js_source_text, $tab_size = 1)
Now, around line 56 where it says:
$tab_string = str_repeat(' ', $tab_size);
change the space to a tab like so:
$tab_string = str_repeat("\t", $tab_size);
That worked for me.

how to fix javascript error in ie7/8?

if you open the page:
http://www.rhino.com/shop/product/the-monkees-the-birds-the-bees-the-monkees-boxed-set
you will see at the bottom of the page, a next and previous buttons, these are navigation buttons, they work fine in firefox but in ie the style is removed from the page when they are clicked!. please advise on what might be the cause of the problem. i have already spent hours debugging :(
When I debug it on IE8, I see a runtime error here:
cd=new Date();
Most likely this does not accord with this piece of HTML:
<li id="cd" class="">CD</li>
IE 'helpfully' launches elements with an id as global variables. So cd is actually a <li> element, and IE doesn't like it if you assign new Date() to it. It would be perfectly fine if IE would treat cd as a normal local variable, but it does not, hence the runtime error.
This piece of code is inside a anonymous function which is really nasty (shown below). But the solution is simple: just write proper functions, and declare your variables. If the anonymous function would have used one line in the top declaring variables like this:
var cd, dc, ...other variable names... ;
it would have worked just fine, because then the cd variable inside the function would have referred to the local variable, not the global cd element which IE thinks referes to the li element with id="cd".
Now it is entirely possible that this is just one of many problems with this page. But I am assuming the first error encountered by IE stopped execution of the remainder of your script, which why some of it diddn't work.
function anonymous(t, z, y) {
dc=new Date('1/1/2000');f=15;ne=8;
if(dc.getDay()!=6||dc.getMonth()!=0){
return'Data Not Available'
}else{;
z=parseInt(z);
if(y=='2009'){f=8;ne=1};
gmar=new Date('3/1/'+y);
dsts=f-gmar.getDay();
gnov=new Date('11/1/'+y);
dste=ne-gnov.getDay();
spr=new Date('3/'+dsts+'/'+y);
fl=new Date('11/'+dste+'/'+y);
cd=new Date();
if(cd>spr&&cd<fl){
z=z+1
}else{
z=z
};
utc=cd.getTime()+(cd.getTimezoneOffset()*60000);
tz=new Date(utc + (3600000*z));
thisy=tz.getFullYear();
var days=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
if(thisy!=y){
return'Data Not Available'
}else{;
thish=tz.getHours();
thismin=tz.getMinutes();
thisd=tz.getDay();
var dow=days[thisd];var ap='AM';var dt='Weekday';
var mint='00';
if(thismin>30){mint='30'}
if(thish>=12){ap='PM';thish=thish-12};
if (thish==0){thish=12};
if(thisd==6||thisd==0){dt='Weekend'};
var timestring=thish+':'+mint+ap;
var daystring=dow;
var endstring=dt;
if(t=='h'){
return timestring}
if(t=='d'){
return daystring};if(t=='w'){return endstring}}};
}
Now debugging where this is comming from is a bit of a prob. My callstack reads:
anonymous JScript
s_doPlugins JScript
anonymous function JScript
global code JScript
The top one is where the actual error occurs.
The root where this is all happening is here:
/************* DO NOT ALTER ANYTHING BELOW THIS LINE ! **************/
var s_code=s.t();if(s_code)document.write(s_code)
Somehow, from here, s_doPlugins is called which contains the actual call to the anonymous function that is causing the trouble. It looks like s_doPlugins is located in s_code.js. If I step through that, I find that the trouble is in this line:
s.prop9=s.getTimeParting('h','-5','2008'); // Set hour
which is line 38 of s_code.js. With a little bit more poking around, I find it is actuall this plugin:
/*
* Plugin: getTimeParting 1.3 - Set timeparting values based on time zone
*/
s.getTimeParting=new Function("t","z","y",""
+"dc=new Date('1/1/2000');f=15;ne=8;if(dc.getDay()!=6||"
...more crap here...
This is at line 95 in s_code.js
As a quick fix i would probably comment out all calls to s.getTimeParting() in s_doPlugins() in s_code.js and see if that fixes your problem. Then, the long and hard but noble taks remains of churning something sensible out of this mess :))
I found this infuriating and discovered that the problem was my site using a div with the ID "utc", which clashes with the s_code JS. 'UTC' is time-related so I can only assume that there's sloppy JS relying on it being something else if present at all!
With 2021 soon upon us, I wanted to resurface an issue with older versions of the getTimeParting plug-in that made quite the stir at the beginning of this year. If you took the easy route and edited the plug-in code instead of upgrading to the latest version, you'll want to make sure that you make those same edits again before January 1.
More information on this topic can be found in this forum thread:

Categories