Change Value Of "y[0]" In The Local Scope Without Affecting "this.a"?
May 5, 2010
I have the following simple script.
Code:
<script>
SPC = {
a : [10],
b : 10,
[code]....
why does the value of this.a[0] increment? I'm assigning "y = this.a" and updating element of "y" as "y[0]++;"?At the same time, exactly the same thing is happening with "b": "z = this.b; z++". Yet, "this.b" remains equal to 10.How can I change value of "y[0]" in the local scope without affecting "this.a"?
If you run it, the alert messageis "pic2", so jquery sees the entire document, but is there a way to easily restrict it to the sub-tree under the current node (in this case the sub-tree under the span node, since that's what was clicked)? Yes, I can do something like alert($("#div1 img:eq(1)").attr("alt")); //undefined as expected
But I am looking for a solution that's more dynamic, so I don't need to hard code #div1.
I have a javascript (HTML internal), which fetches and executes a file in another web address... but I do not want it to do that, i want this script to execute is function directly from my local disk (file location).
I'm making a site for my g/f – She's writing angel messages for each day of the year, 1 message on 1 page and these (eventually 366 pages) are accessed from this one page:
[URL]
This menu page uses JS to slide to particular months. The place I'm stuck is how could it be possible to go from a particular days message, back to the month of that message in the per-datum page (having the page slid to that month)...
for clarities sake... “Terug” means “back”, so for example to click on october 22nd message, read the message, and terug back to Octobers seeds rather than back to the per datum pages month selection calender.
My best effort so far is to put this in the a tag:
-1000px would be Januarys terugs, -2000px Februarys terugs etc to have it already slid to that month, this code works when on the same page, but when its on the message page before loading up the per datum page it seems to lose this setting.
I notice when I design a basic HTML page, it is affected if a WinXP user has Large Fonts set in their Control Panel. However some pages, e.g. www.bbc.co.uk/news are unaffected by this I think. They are definitely not affected by changing the font size within the browser.
Because I want to add tables, which will word-wrap incorrectly if the font size is changed, how can I disable allowing the browser to do this?
Basically I want the font size to stay how I designed it. If they need it bigger, they can use the zoom option in more recent browsers. Code:
I have javascript in an iframe that controls showing/hiding of divs. This works great, but after the javascript is executed it changes the behavior of anchor tags of the main window of the website (namely - links in the main window all open in new windows and not in the specified target iframe). I've searched and searched and looked up all sorts of things, rendering me a confused and frustrated fellow.
I have a listing of items on the page and each one has a <div> with a description in it. There are two versions of each description, one short and one full length. I like the user to be able to swap between them by clicking the 'more' and 'close' links. The problem with what I wrote is that it applies to all the divs on the page instead of the specific one that was clicked. What can I change to correct it?
I have a jquery script converting a XML file into html and part of the script is set to display an image based on the value in one of the elements. It's almost working as planned except that it affecting the preceeding record instead of the record that meets the conditions of the if/else statement.[code]
I'm quite new to jQuery and have this working, though I'm fairly sure if this won't be an efficient way to do this. I have a form with each day of the week, and each day has a choice in the number of slots which then shows the correct number of slots for entry for that day.
The form: <table border="0" cellspacing="0" cellpadding="0" class="DataTable"><tbody> <tr> <th> Day </th> <th> Slots .....
This is simplified just a little as there are more than the 1 input in an 'Entry Section' (eg .Entry3). The jQuery I have working here is: <script> $(document).ready(function(){ $("#frmSlots1").change(onSelectChange); }); function onSelectChange(){ .....
That's working fine, except I do want to have 7 days so that would be quite a lot of replicated jQuery code. I am thinking there will be a way to identify the select item with the others so jQuery will know which to adjust without having to spell every single item out. Is there an easy way to achieve this in the above?
I'm using window.open to create a secondary window, and everything is working fine with that. My problem is that as soon as that window is opened, the parent window scrolls to the top of its page. So when the user closes the secondary window, they've lost their place in the parent document.
Is this normal behavior for window.open? Is there a quick way to prevent it, or is it more likely a problem with my Javascript?
I'm trying to write an 'each' function for a JavaScript array that behaves like Ruby's Array#each. (It doesn't matter if you know Ruby to help with this question.)
My problem is the scope of 'this' inside the iterator callback. I would like it to be the same as the object that called the each() on the array. Right now I have to do that with a closure or an explicitly-passed 'this' scope. For example:
Array.prototype.each = function( inScope, inCallback ){ for ( var i=0,len=this.length; i<len; ++i ){ inCallback.call( inScope, this[ i ], i ); } }
Inside the each() function, arguments.callee.caller would give me a reference to the showInfo function object. What I am looking for is a way to access the scope of the 'this' receiver within that particular invocation of showInfo(), so that I can use it in place of inScope without having to pass 'this' each call.
In the method nextImage, I can't figure out how to access thumbs. It keeps coming back as undefined. (Using Firefox)
function runPortal(portal_number){ // there are multiple runPortals on each webpage this.portal = document.getElementById('portal'+portal_number); // represents the div that holds the images this.thumbs = this.portal.getElementsByTagName('a').length; // represents all the images within the div that will be rotated this.length = this.thumbs.length; // that's how many images will be rotated // Hide everything for (var j=0;j<this.thumbs.length;j++){ if (j==0) continue; // Don't hide the first one this.thumbs[j].childNodes[0].style.display = 'none' } this.nextImage = function (){ // there are a fixed number of images to rotate. Start over if (this.i >= this.length){ this.i = 0; } // One fades away, the next appears Effect.dglPuff(this.thumbs[this.last].childNodes[0], {duration:.6, from:.7}); Effect.Appear(this.thumbs[this.i].childNodes[0]);
// iterate to the next image for the next run this.last = this.i; this.i++; } // Set up the image rotator // here is where I started guessing // thumbs needs to belong to the object rotator, I guess.
this.rotator = new PeriodicalExecuter(this.nextImage, 4); // This object runs the function every 4 seconds this.rotator.portal = document.getElementById('portal'+portal_number); // represents the div that holds the images this.rotator.thumbs = this.rotator.portal.getElementsByTagName('a'); // represents all the images within the div that will be rotated this.rotator.length=this.length; // that's how many images will be rotated this.rotator.i=0; // the counter for what image we're one this.rotator.last=0; // the counter for the previous image
function attributes() { var attr1 = arguments[0] || '_' var attr2 = arguments[1] || '_' return ( function (el1, el2) { var value1 = el1[attr1] + el1[attr2]; var value2 = el2[attr1] + el2[attr2]; if (value1 > value2) return 1; else if (value1 < value2) return -1; else return 0; } ); }
var a = [ { a:'smith', b:'john' }, { a:'jones', b:'bob' }, { a:'smith', b:'jane' } ]; a.sort(attributes('a', 'b')); for (var i =0; i < a.length; i++) { document.write(a[i].a + ', ' + a[i].b + '<br>'); }
My question is, are attr1 and attr2 guaranteed to exist through the lifetime of a.sort(attributes('a', 'b'))?
As I understand it, the anonymous inner function reference I am returning is a property of attributes(). As such, when I return a reference to the anonymous inner function, the outer attributes() function must continue to exist (as must attr1 and att2) until there are no further references to the inner anonymous function.
As a result, there is no danger of attr1 or attr2 "disappearing" during the repeated calling of the anonymous inner function.
Is my explanation basically correct, or am I deluding myself and I'm just lucky that the garbage collector hasn't recovered attr1 or attr2 while the sort is still going on? In other words, is the behaviour I'm seeing consistent and predictable, or should I change my approach?
I have a script in which a function launched by a START button continuously calculates and writes a value to a text box. The calculation is done in a for loop. In the loop is a conditional that is a global variable, a boolean. If the boolean is true, break ends the loop (or is supposed to!). A STOP button has an onclick function that sets the global variable to true.
What happens, though, is that the function for the STOP button is not executed until the for loop reaches the maximum value set for i. Anyone know how you can get one button to stop a process started by another?
I had a need for a two dimentional array. Looking for this solution, I ran accross a statement than all Javascipt arrays were arrays of objects. So I created a function prototype, at least thats what I was calling it:
Next I did: var myobject=new objRow("1", "1234 Main St.", "Apt 101");
At this point I was able to see myobject.addr1 or any other variable in the object instance.
Now I added this object to a table. var aryTestTable= new Array(); aryTestTable[0]= myobject; At this point I could see aryTestTable[0].addr1 Next I tried an additional object myobject=new objRow("1", "1234 Main St.", "Apt 101"); //with different data And added it to the table aryTestTable[1]= myobject; Where I could see: aryTestTable[1].addr2 or any other variable.
so far so good. Then I started the actual application code where I was reading a database table and creating the objects and adding them to the table. This was in a for loop wherein the myobject=new objRow("1", "1234 Main St.", "Apt 101"); was instantiated.
After the for loop was finished, I could not access the data in the table - undefined.
So my questions are: Have the my object instances popped off the stack? and What is the alternative way to implement this table of rows of values.
I'm showing a form in a Simplemodal dialog in combination with ajaxForm() to redirect the resulting page to another DOM element. The success function of ajaxForm() closes the modal dialog.The resulting page in the other DOM element has no access to the jquery function $(). When I load a page using ajax into the DOM element there is no issue access the jquery function, this only happens when I redirect the resulting page from the ajaxForm() function.
I've just realized that in Mozilla pointer variables always have local scope in a function. Unlike IE. I wondered if Mozilla was able to do it in some other way? readXML() is an init() function which might be a constraint - I'm no javascript expert.
// The following won't work in Mozilla.
var record;
function readXML() { record=xmlDoc.getElementsByTagName("record"); }
LiveSearch.prototype.initialize = function() { $("#thebutton").mousedown(function() { //when we click the button alert(this.theglobal); }); }
$(document).ready(function() { var objSearch = new LiveSearch("globalvalue"); }); </script>
On page load I create a new LiveSearch instance and it assigns theGlobal = "globalvalue" and proceeds to initialize(); At this point Im using JQuery to setup an onmousedown event on a button on my page with id="thebutton". When I click the button the alert comes back with 'undefined'. How can I get direct access to my theglobal variable? Code:
I think I've had JavaScript variable scope figured out, can you please see if I've got it correctly?
* Variables can be local or global * When a variable is declared outside any function, it is global regardless of whether it's declared with or without "var" * When it is declared inside a function, if declared with "var", it's local, if not, it's global * A local variable that is declared inside a function is local to the whole function, regardless of where it is declared, e.g.:
function blah() { for(var i ... ) { var j ... }}
i and j will both be visible within blah() after their declaration. * the notion of "function" in this context also applies for this kind of construct:
var myHandler = { onClickDo: function() {
in the sense that whatever one declares inside onClickDo with "var" will only be visible inside onClickDo. What else, am I missing anything?
I've discovered a scenario where Safari 1.3 (I need to make my stuff compliant with 1.3+) gets confused about the scope of local variables WITHIN functions that were created in dynamic script blocks. I've made this example where function def has a local i variable in a loop, and it calls function abc which also has a local i variable in a loop. What happens is that Safari is not respecting the scope and is allowing the called function to corrupt a local variable in the parent function
Here's the whole test page including html tags. If you try it you'll see that IE and Gecko both produce the output "in abc" twice, because the def function correctly gets to call abc twice. On Safari, i gets corrupted, and abc only gets called once... Any ideas what I can do to prevent this? Code:
I will do my best to explain this one and sorry if the title isn't that great. I am trying to write a javascript object and it is my first time, so it isn't that great and of course I have trouble.The object is suppose to populate a dropdown (popMake()) and then add an onchange event (checkValue()) to it. It seems to do this fine but when the select box is changed, I no longer have access to object, it's parameters, etc. In checkValue I don't have access to this.currentMake which was just set or anything (already said that).I believe it has something to do with scope (possibly closure, but I didn't see how it would fit her). So how can I do something like this and still have access to the object after the click?Code below:Code:
window.onload = function() { var mm = new makeModel(); mm.popMake();