Redirection does not take effect - javascript

In one of my projects, I have window.location.href='index.php'. That project is big and it is absurd to paste whole code.
In it I have attached event handlers, some AJAX calls and few custom animations. I had solved this in other manner, but I would appreciated few suggestions why that command didn't work?
When I used FireBug to inspect code, and go step by step, redirection was successful.

Try to add your code to the onload handler or, if you use jQuery, in the ready handler:
$(document).ready(function() {
window.location.href='index.php';
});

Related

Window onload event misses sometimes

I am working on a page where I have to make interface changes on window.load. I am using
jQuery(window).load(function () {
//all my code goes here
});
It works fine most of the time but very randomly few time, it doesn't execute at at all. I don't even get any error in the console. It is so random that I can't event reproduce this problem. My question is that is there anything more reliable than the window.onload event to make interface changes?
I also apologies in advance that I can't share the link.
You can avoid jQuery and use plain javascript. You can use DOMContentLoad (https://developer.mozilla.org/pt-BR/docs/Web/Events/DOMContentLoaded) event. Or addEventListener
window.addEventListener('load', callback)
Take a read here.

Javascript/jQuery How to Attach a Load Spinner to the Syntax Highlighter Plugin

I am using Alex Gorbatchev's SyntaxHighlighter plugin to beautify XML messages. It works like a charm for small messages, but takes quite a bit of time for bigger ones (~10k lines) and the page gets frozen until the plugin finishes work. Now I would like to attach a load spinner to the syntaxhighlighter, so the "Loading.." message will be shown while the plugin does work and will go away once the plugin is finished. I already have the spinner, I just don't now how to hook it to the SyntaxHighlighter. All the information on load spinners I found was related to using them with Ajax calls. Going through the plugin's API did not help me either, I was looking for some kind of an event to signal completion, but since I am new to JS/jQuery I could easily miss it.
So my question is, how do I bind a load spinner with the SyntaxHighlighter plugin? Should I somehow use jQuery deferred object, or manually attach events to the plugin?
Any information will be highly appreciated.
<script type="text/javascript">
SyntaxHighlighter.all();
$(window).load(function () {
$('#spinner').hide(); // Hide after load is completed
});
</script>
Syntax Highlighter does not run at .ready but completes when .load() is completed.
So by hiding your element (here id="spinner") when load is completed using jquery.
Took me a while to figure this out as well.

Using data return from sharepoint assetimagepicker

I'm working with Sharepoint 2007. I use the built in AssetImagePicker.aspx and I need to retrieve the image url from that page and use it in my custom webpart.
I want to run my javascript code to run when clicking the OK button of the AssetImagePicker.aspx but I can't find a way to do that.
Does anyone know how to do this?
Thanks
Not sure if I'm understanding the question properly, and I know nothing about Sharepoint (and AssetImagePicker.aspx) but it sounds like you want to add an event handler to the OK button: to run a function when the click event of the button is fired.
You can either code event handling yourself (see W3Schools or Quirksmode for examples of this) or (probably better) use a library like jQuery.

Javascript document loading event

Is there any javascript document loading event which which will activate after: the html is loaded but the not displayed in the browser?
HTML is displayed as it loads, so no, there's not an event like that. Are you perhaps looking for something like jQuery's $(document).ready()[API Ref] event?
If not that, then give us a little more context and we'll try to help you out.
You could load the HTML through an AJAX call and display it when you feel like it, but that would be a (slightly) more manual process.

window.onload equivalent for Ajax applications?

I'm using window.onload to call my JavaScript code that has to be executed after the page is fully loaded. From what I read this is the recommended method to call such scripts.
However, it does not work for some Ajax sites, www.bing.com for instance - window.onload is called before the pages is fully rendered.
Any suggestions?
The short answer is that there's no general way to solve this problem (right now).
The definition of a "page" is pretty fungible when AJAX comes into play - it's pretty hard to tell the difference between AJAX that is intended to be part of the initial page load, and that which might not be. So, browsers are left on their own to determine when window.onload() should be fired, and it doesn't always end up where you want.
Luckily, most people don't need a general solution, but rather a specific one. If you care about bing.com, then you can probably look at how bing.com works and design your code to trigger when the site reaches a state that you find acceptable.
I've wrestled with this a couple of times, and my usual reason for needing some sort of onload event triggering is to be able to interact with the HTML DOM and have getElementById, getElementsByTagName, or various other DOM selectors, return something other than nothing.
Once again, I'm not sure of the exact problem you are trying to solve, but if you must use some sort of onload, and it's because of DOM traversal of some kind, you can cheat a bit with the following sort of code:
window.onload = pageChecker;
function pageChecker() {
// Onload has fired, but we don't trust it.
// Check to see if our deepest nested element can be found
var testElement = document.getElementById("importantElement");
if ( !testElement ) {
// Try again in a bit, adjust timeout to what your users
// can tolerate
setTimeout(pageChecker, 50);
}
else {
//
// ... the element exists, run important code below ...
//
}
}
You might be able to listen for changes and requests by globally listening for DOMSubtreeModified and readystatechange events, and use those events instead of the load event for whatever you are trying to do.
I'd recomend using jQuerys $(...).ready() method.
In addition to the answer from Rasmus Kaj and if you are using jQuery, I would direct you to take a look at Global Ajax Event Handlers.
jQuery.fn.ajaxComplete (http://api.jquery.com/ajaxcomplete/)
jQuery.fn.ajaxStop (http://api.jquery.com/ajaxstop/)
Note, that this has nothing to do with the native onload event.

Categories