The issue is now resovled :) Thanks for everyone's help and attention!
I'm getting the JS error "Unexpected call to method or property access" in IE6 intermittently on the line "oAutoCompleteTextBox.focus();". Hopefully, someone has seen this issue before and can provide some insight on how to solve it. Below is the context of the usage.
$(document).ready(function () {
...
oAutoCompleteTextBox = GetElement('<%=this.AutoCompleteTextBox.ClientID%>');
...
SetupDefaultValues();
}
function SetupDefaultValues() {
...
if(canFocus(oAutoCompleteTextBox)) {
oAutoCompleteTextBox.focus();
}
}
My 1st post on stackoverflow - YAY!
OK, so the issue was that the jQuery $(document).ready() event isn't fired on updatepanel async postbacks. The solution is to refactor the function definition inside the ready() into an explicit function definition (i.e. function pageReady(){...}) and add the new pageReady() eventhandler to ASP.net Sys.WebForms.PageRequestManager endRequest event which is only fired on async postbacks.
So the code now looks like this:
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(pageReady);
$(document).ready(pageReady);
function pageReady() {
...
oAutoCompleteTextBox = GetElement('<%=this.AutoCompleteTextBox.ClientID%>');
...
SetupDefaultValues();
}
function SetupDefaultValues() {
...
if(canFocus(oAutoCompleteTextBox)) {
oAutoCompleteTextBox.focus();
}
}
Thanks for everyone's help and attention - took a while to figure out, I'm just glad it's resolved :)
Is oAutoCompleteTextBox declared globally? You're setting it in the document.ready function but trying to use it in another function.
are you sure its a textbox? what does "canFocus" function do? alert on that line, oAutoCompleteTextBox.tagName, then if it is "INPUT" alert .type, if it is "text" then you have problem :) knowing IE6, it might be a timing problem nothing but, if you call setupdefaultvalues in a settimeout of 10 seconds, i MIGHT work
Related
I want to call a function which is in my plugin.
I overloaded a function but I want to call another function from it.
$("mySelector").Myplugin({
My_function : function (){
*do some stuf*
function_from_the_plugin();
}
});
An error appears that says "Unknown function". I think it do not search the function in the plugin but outside.
Do you know how I can do?
I already try with "this", a variable corresponding to my plugin object.
If you have an idea please share it with me.
Thank you for reading this post.
Add this.function_from_the_plugin(); in code where Myplugin() is initialized.
I apologize for this rather BASIC question, but I am at whits end here!
I have a javascript function that I want to run when the page loads... simple right? I have tried putting the function in doc readys, window readys, putting it before and after the main function, etc etc. NOTHING seems to work.
Code so far:
function calcme() {
DA FUNCTON
}
$(document).ready(function(){
calcme();
$("input").bind("keyup", calcme);
});
Please note... the keyup bind DOES work, but I need the calcme function to load on page load. Thoughts?
UPDATE: As per request, here is the full fiddle. http://jsfiddle.net/vpsSA/
Problem found: The calcme() function assumes it is called from the context of one of the inputs and uses this.value inside. So, when you call it without any arguments, it obviously fails. This can be solved by trigger the keyup of each of your inputs, instead of calling calcme() directly. See the fiddle below.
Working fiddle: http://jsfiddle.net/vpsSA/1/
In your ready() handler, the bind statement comes after the caclme() call. And as you mentioned the event binding worked. This means:
a) calcme() was definitely executed on load. There is no other way since you've mentioned that the binding statement which comes after the calcme() call worked as expected.
b) calcme() did not throw any JS errors - if so, it would have stopped at the error and the event binding would not have taken place. Maybe it threw a warning, which you'll be able to see in your JS console.
c) Since you haven't provided whats inside calcme(), we can't say for sure. But what it looks like is some sort of condition failure because of which you did not get the expected result from calcme() when running on load. Are you using anything inside calcme() thats initialized after running it on load. I would suggest putting in a debugger; statement as the first line in your ready() handler and tracing it in Firebug or Chrome.
try this:
function calcme() {
try {
//your code
}
catch(e) {
alert('error: ' + e);
}
}
if (typeof $ === undefined)) {
alert('not jquery');
}
$(document).ready(function(){
if (typeof calcme === undefined) {
alert('calcme not exist');
}
else {
calcme();
$("input").bind("keyup", calcme);
}
});
I am having some issues with my homework assignment. I don't know how to start it or how to do it. I don't need the entire code just what is needed for what is being asked. Please can anyone help me on this. I need it ASAP.
Write a custom error handling JavaScript function called processErrors that handles a custom error by assigning it to the onerror event handler. Include the block of JavaScript statements needed to pass the arguments sent by the JavaScript interpreter into the processErrors function, send an alert message with the agreements, return, and write the event handler that calls the processErrors function.
Please can anyone help me.
function handler (processErrors); { onerror="alert ('There was a custom error')"}
This type of stuff can be a bit challenging when you are completely new to it, so Ill help. In an html page, put something like the following
<script>
function handler(event) {
alert(event);
}
</script>
note that example is not complete according to your question. what this does is declare a js function, 'handler', that takes an argument, 'event', and then pops up the event. This is done is 'script' tags, which you should also expand according to your research.
The next thing you will have to do is assign the function defined above that you complete, to the onerror event of some dom, as shown here: http://www.w3schools.com/jsref/event_onerror.asp
Look here for more guidance on js.
http://www.w3schools.com/js/default.asp
You also might want to google things like: "html script tags" and "javascript event handlers"
Ok I have a flex app and I am adding a callback method like this:
private function init():void
{
ExternalInterface.addCallback( "playVideo", playVideo );
}
private function playVideo(videoSource:String):Boolean
{
videoDisplay.source = videoSource;
return true;
}
I am calling it with javascript like this:
function showVideo(video)
{
document.getElementById("video_overlay").style.display = "block";
//alert('no error');
document.getElementById("MiniMacVideoPreview").playVideo('https://www.kranichs.com/instore/minimac/videos/'+video);
}
I get this javascript error:
Object does not support this property
or method.
However if I uncomment and run the alert first. I get no error and it works perfectly.
My first thought was that the alert was buying time until the script could execute, so i tried to run the script inside a setTimeout() but did not work.
Any ideas?
Thanks!
I would try placing your code in something like jquery's
$(window).load function. I have a feeling that you are exactly right. By the time you close the alert, the dom and contents are finished loading and you can make your ExternalInterface callback method.
$(window).load
Otherwise, if you are using swfobject, you could do something like
swfobject.addLoadEvent(function() {
$("#swf_id").get(0).inited(callSomeOtherFunction());
});
I'm experiencing difficulties trying to invoke document.ready( function() {}) in my unit tests. Suppose I have multiple of them in my javascript file, and one of them called inside a named function i.e.
function myFunction() {
$(document).ready(function() {
//...
});
}
How do I actually invoke them in my unit tests so I can actually test them? I'm using JsTestDriver to unit test my javascripts.
Thanks.
If it's a unit test, I'm guessing you check the function outputs when given certain inputs?
Here's my opinion:
You should prepare for the case where document.ready is called and the case where it isn't.
So your unit test should run each function twice - once to simulate a pre-ready call and one to simulate a post-ready call. That is, you should have one run-through where anything that happens on document.ready DOES run, and one run-through where it's just ignored (presumably to be called later on in the lifecycle).
EDIT:
Just reread the question and understood it a bit more. You could just override $(document).ready to do what you want it to (which is NOT to wait for the DOMLoaded event to fire, but instead to run the functions immediately). This snippet will replace the $(document).ready function with a function that does exactly that. It should run before any unit tests.
var postReady = true; // or false to ignore the function calls.
jQuery.fn.ready = function(fn)
{
if(postReady && fn) fn();
}
Example test case:
<html><head><title>whatever</title>
<script type="text/javascript" src="/JS/jquery-1.3.2.js"></script>
<script type="text/javascript">
var postReady = true; // or false to ignore the function calls.
jQuery.fn.ready = function(fn)
{
alert("We stole ready!");
if(postReady && fn) fn();
}
$(document).ready(function()
{
alert("The function is called.");
});
</script>
</head><body></body>
</html>
You know document.ready... works so just start with calling the functions within it. Ideally, if you just have an init function called by the ready function then you call one function, it does what you need, and you can continue with your tests.
You can take unit testing too far, in this case you need to ask yourself what you are testing, and why. The JQuery document.ready function works, and work well (you know this because it's been tested by many many people).
I would assume the trick would be to, instead of creating an anonymous function, naming one, and using it.
//So instead of this...
$(document).ready(function() {...});
//Do the following
$(document).ready(my_function);
Then you just test my_function and make sure that it is working. Make sure that you test the functions in the order their going to be loaded for an accurate test.
I suggest you to refactor the code. Even if you find a way to call it, it will be hard to understand for other developers.
Also (IMHO, I am not quite sure) you have to call the ready handlers even after the pages ready event was triggered, because if you "install" the ready() handler, if the document.ready event was already trigger, jquery calls that handler immediately (so it never loses that event, even if your code added a handler too late - that is, way after document.ready was still done).
Couldn't you just create a user my_on_read() event ? Or something the like?
Well, in the end, please just take care of ready() events and handlers that will be installed after the document.ready() is already done :)
Part of the answer to this question can be found here.
Below is the sample code to answer this question based on the above answer:
myFunction();
$.readyList[1]();
The index assumes that there is only 1 document.ready function in the source file. Index 0 refers to something else which I believe is info on the browser.