I have an ajax call which I need to then load an instance of swfupload on. As far as I'm aware, swfupload has to be loaded on window load and code stuck in the head of the document.
Does anyone know how to load swfuploader from an ajax call?
Thanks!
It's amazing what a bit of sleep can do...
Here is how to load swfupload via an ajax (ish) call.
Scenario:
I call some html via AJAX and write it to my document. This HTML contains the elements for swfupload. Once the elemtns are written to the document, I then create the settings var and call a new SWPUpload instance (swfu = new SWFUpload(settings)). You need to ensure that all the js libraries are included within the parent page which the ajax is being called from. Easy!
Related
I'm running into issues with DTM and the timing of referencing a js object. I noticed that at times, DTM doesn't have access to the js object the web application (ASP.net MVC framework) creates. The js object is created before any of the js from DTM loads but I started having to use "settimeouts" in a few spots to ensure I had context to the object but this is now becoming increasingly more difficult to manage as I'm having to do this in a number of locations.
I was wondering if anyone had any advice on how to delay the loading of the DTM files until I know that I have access to that object? I have a bootstrap file that loads the appropriate DTM files. I was thinking about possibly putting the delay in the bootstrap loader file but I still don't like that solution as I'm also concerned about load time of the pages. Ideas?
-Thanks!
You're right. This can be tricky. Essentially its a race between the DTM page load and execution of your code.
If this is something you need to control on page load, one solution would be to "abort" the original AA request on page load and then, when your object exists, call a direct call rule that will send the data.
I might try something like this:
//Page Load Rule - Adobe Analytics Custom Code Section
s.abort = true // cancel the initial image request on page load
//Check for ASP Object
//Can be done within AA custom code or a custom JS tag
if (MY_OBJ_EXISTS) {
_satellite.track('MY_RULE_TO_SEND_DATA_TO_AA')
}
The same concept can be applied if you have ajax that needs to load before you send data to AA. You can abort the initial call, listen for a callback and send data via a direct call rule.
Hope this helps.
Currently I have an website where each page on the site has a corresponding js file that contains a class (well function) defined in it. Currently when the user navigates to another page (which is just ajax content) I remove the old script tag from the page and add a new script tag for the new page. This is working though I was wondering if there could be any problems with this or better ways to do it? I've been thinking of using an AJAX call (XMLHTTPRequest) to get the new js file then using eval to initiate the new page.
What you are doing makes sense because the browser can intelligently cache the js.
Getting the js yourself might prevent the possibility to cache.
What is the best way of loading the content of a html page into another webpage?
Lets say i have a front page (index.php) where i can choose between PHP and javascript and i dont have to care about compatibiliy, mobile users etc.
In the past, i used the include()/include_once() function in php to do something like this to load the content of my content, which i have put in the content.html, into the div-container on my index.php:
<div id="content">
<?php include("content.html"); ?>
</div>
But i could do pretty much the same thing using javascript/jquery:
$("#content").load("content.html");
But what is the best way to do it?
I know that you cant really compare PHP to javascript. Normally i would only use javascript for user interface stuff. Especially with jquery's load() function its very easy to load content from a file into a div and replace PHP's include(). But just because it can be done, doesnt mean in should be done.
Is there any kind of best practice? What are the pros/cons on using javascript for this?
Assuming that the HTML file you are including is not dynamic (not created in PHP, just static markup) then the PHP include is the preferred approach.
By using PHP include() the content will be on the page when it is first loaded in the browser, whereas a $.load() call will load after the page data is read by the browse, then javascript begins to render, then jQuery is loaded, THEN the $.load() is triggered asynchronously.
Also, $.load() requires jQuery, where as PHP include() is a built in function that is much faster than loading jQuery and firing of an asynchronous request via $.load();
If you need content to be loaded after the page first renders (i.e. as the result of user interaction) then $.load() is needed.
I would recommend that you read about the difference between synchronous (i.e. PHP include(), $.ajax({ asynch: false })) and asynchronous (i.e. $.load(), $.ajax()) resource loading, or network requests.
Here's one place you could look: http://www.w3schools.com/ajax/ajax_intro.asp
I'm opening a colorbox within jsp as follows:
$.colorbox({maxHeight:"100%", href:'<c:url value="/html/dashboard_report.html" />?organization=${organizationKey}&category=${chartCategory}&severity=' + selection[0].row});
within my dashboard_report.html (which IS html, not JSP) using javascript I want to get the request so I can use the querystring to make further ajax calls to populate some data. window.location.href doesn't work because the window location hasn't changed.
Do I just store the applicable parameters in global vars when the colorbox is opened and read those in the colorbox content or is there some other way?
You can't use JSP to execute JS. Do you mean you're writing the JSP, and it's creating the script for you, then trying to execute?
In above case you can do something like:
$.colorbox.settings.maxHeight = "100%";
$.colorbox.settings.href = "http://someURL.com";
at the begning of your script and leverage them later dynamically, and those are only a couple of the available properties you can set to defaults... I'm not sure what you're trying to do can't accomplished without an nested AJAX call even though I'm opposed to doing that when possible.
I read (somewhere else on this site) you can't reload (or inject javascript) onto a page that is already rendered.
Is there any other way of doing this. For instance an iFrame?
I have a recent comment widget.js and I need to constantly get it to reload without reloading the whole page.
Any ideas?
edit: The site has recent comments on it and they are displayed via a recentcomment.js
Once the page is loaded it doesn't update itself unless you reload the page. I want it to update itself, a way to do this is to just reload the js file on the page, correct?
Why do you need to do this? It seems to me that there's probably a more appropriate solution to your problem.
But to answer it:
var elm = document.createElement("script");
elm.src = "Widget.js";
document.getElementsByTagName("head")[0].appendChild(elm);
Hope I didn't write any mistakes...
Rather than reloading the file, you can have all the implementation of the file contained in a function and then call the function every minute using the setTimeout() function.
Alternatively, if you want to reload it because the content of the file might have changed, it would probably be better to move that part of the code out to some external file and then use a function (running every minute with setTimeout()) to load the new content you need.
You can make cross-domain AJAX requests using certain methods, so you could make an AJAX request for the script file and parse it yourself. Parsing it yourself probably isn't an optimal solution, but it looks like you're dealing with a brain-dead service provider anyways.
Look at this guy's jQuery mod for an example:
http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/
Once you get the data from the 3rd party, you could probably use some combination of regex and JSON parser to extract the comments.