Possible to append a ActiveX control to a page using javascript? - javascript

I'm trying to append an ActiveX control dynamically to a page using jQuery. The append is successful; however, the control doesn't initialize when it is done this way. I believe IE calls the OnCreate method of an ActiveX control when a page that contains a control has finished rendering. The problem is that the tag is not present on the page until after rendering is finished, so OnCreate is never called.
I'm not sure if that's the problem, it's just a guess. Does anyone have experience with this? Is it possible to force IE to call OnCreate at a specific time?
The control works fine if the tag is in the html. The only time I see problems is when I add the object to the page via javascript.
Update: I need to know what IE does when it encounters an
<object>
tag on the page at render time. The control works fine in that context, so IE is calling something at that time. I need to invoke that manually after I've added the control to the page post render.
Thanks, Pete

You can instantiate the control in a totally cross-platform-unfriendly manner using new ActiveXObject(ProgID). ProgID is a string of the form "appName.typeName". e.g.,
var excel;
excel = new ActiveXObject("Excel.Application");
...
The example will only work if excel is installed on your machine.

I had a similar problem to yours today with a java applet under IE. My workaround (i wanted to put the applet after page has finished rendering) was to dynamically create invisible iframe with src pointing to simple html page with my applet. After loading iframe i called it's parent to notify that the applet was loaded.

Related

Suitescript within iframe

I have a suitelet that is adding an inlinehtml field which contains an iframe. The problem is when I try to use the API functions I get an error in the browser.
For example just trying to do
var value = nlapiLookupField('customer', custid, custentity_mycustomfield');
Results in nlapiLookupField is not defined. Is there a way I can access the API by writing functions within my HTML page being called as an iframe? I'm open to suggestions, if there is a way I can include the functions as part of the suitelet and call them from the iframe I would do that but so far have not gotten it to work.
If your iframe is all custom HTML then you won't have the suitescript client libs loaded.
I take care of this one of two ways:
Call parent.nlapi...
Load your iframe from a suitescript. This can be a really simple form with room to load your custom HTML
Of course if you go the second route why do you need the iframe? You carload quite a bit of custom HTML into a div inside an inline HTML field. In my experience that simplifies things quite a bit.

JS on ajax-loading pages getting out of sync with server-side code and HTML

I have a website that loads mostly using AJAX calls. The javascript and CSS files are only loaded once when the page first loads.
My issue is that the javascript/CSS can get out of sync with the HTML and server-side code. The page can be using an old versions of the javascript file (from when the page first loaded) while the server-side code and ajax-loaded HTML files always use the latest code and files.
What are some strategies for dealing with this?
I have considered polling the server at set intervals and asking if there is a newer version of the JS. Then, if there is, reloading the page. But, it seems that this can get ugly, with the page suddenly reloading at awkward moments instead of, for example, as the result of a user-initiated call.
Also, there are some changes to the javascript that do not necessarily require that a page be reloaded. For example, the changes might affect a different page/module than the one that the user is on.
Re-loading the javascript with every Ajax call is not viable
I can imagine ugly solutions to this, but thought I'd ask first.
EDIT (in response to comments and suggested answers)
The only way to get the JS back into sync is to reload the page, which then loads the new JS. Adding new JS to an old page won't work as it doesn't get rid of any old functions, listeners, etc. I'm not asking how to reload a page or how to load javascript. I'm asking for a strategy of knowing WHEN to do it, especially in a way that does not seem awkward to the user. Do people incorporate polling to ask if there is a new JS version? Do they then suddenly (from the user's point of view) reload the page? Do they poll even when the tab is hidden? Is this a problem for the server? Where do they keep track of the latest required JS version? Or, do they ask with every AJAX request - hey, should I reload? Did they write a special function for that? Do they keep all new html/server code backwards compatible with the js?
Someone who has dealt with this, how do you do it?
Two possible solutions include
calling $.getScript() to retrieve, update variables at document from at server-side to match variables at document before calling $.ajax(/* settings */) ;
alternatively could use web workers to update original document variables to match server-side variables at beforeSend of $.ajax(/* settings */)
At result of first step of either approach, abort $.ajax() call, call error handlers, notify user, send message to server about error.
var head = document.getElementsByTagName("head")[0],
scripts = {};
function load_script(name){
var myscript = document.createElement('script');
myscript.setAttribute("type","text/javascript");
myscript.setAttribute("src", name);
if (scripts[name]) head.replaceChild(myscript, scripts[name]);
else head.appendChild(myscript);
scripts[name] = myscript;
}
// the first call to load the script code
// and then call if you decide to upgrade a newer version
load_script('js1.js');
load_script('js2.js');

How to preserve loaded javascript content in a WebView when loading a new page?

In a WebView I can load javascript code by running view.stringByEvaluatingJavaScriptFromString or loading the code directly in the JS context you can retrieve via view.mainFrame.javaScriptContext.
However, when I navigate to a new URL (be it in the WebView.mainFrame or in Javascript, e.g. by setting window.location) all my javascript code is lost and I have to load it again. This is especially a problem when I navigate in JS code and want to continue after that in my script. Is there a way to avoid clearing all JS and in particular prevent stopping the current JS code?
As it turned out it's not possible to prevent the context recreation. Instead I ended up with a standalone JSContext instance (manually created) and a WebView instance I set as variable in that context. This way you can run your javascript in the standalone context but use the WebView to do all the web work. The big disadvantage is that you cannot simply act on the DOM but have either to provide manipulation functions in your context (mapping to the the WebView instance) or create an extension for all WebView class (DOMDocument etc.) that publish their methods to JS. That way you can directly act on the swift/obj-c classes.

Modify an UpdatePanel by calling server-side code from client code

It's possible that there's no way to do this, but I figure I would ask. I'm relatively new to asp.net, having played with it for about a week now. What I have right now is a page that calls a web service, polls it until it's done (with progress displayed in an UpdatePanel), then hides the progress text and instead displays the result (a recursive list of files with some metadata) by creating a TreeView and adding it to the UpdatePanel. What I would like is for clicking a node in the TreeView to update a second UpdatePanel with extended information (obtained server-side) about the node that was just clicked on. I don't see any way to call a codebehind function by clicking a TreeNode, but I can call Javascript code by setting the node's NavigateUrl to "javascript:function([the full path of the node])".
At this point, though, I'm kind of stumped. StackOverflow is full of correctly-answered questions about how to call back into the codebehind from javascript (using a WebMethod, or equivalent), but apparently you can't call code that isn't static, which would mean I couldn't modify the page itself, or for that matter, access the session or page state. StackOverflow is also full of questions about how to have javascript request that an UpdatePanel refresh itself (__doPostBack()), but without any way to communicate to the server what was clicked on, the UpdatePanel wouldn't know what to display.
Thus, the question, which I'm hoping has an answer: am I missing some clever way to have javascript on the page trigger a server-side function capable of taking a parameter and using it to do a partial postback of a different UpdatePanel?
Thanks!
Actually it's quite straightforward. Just place a LinkButton inside of second update panel (it can have an empty text and thus be invisible), in JavaScript call client-side .click() method of that control, and in ASP.NET handle server-side OnClick event.

Run JavaScript After DataBind

I have a ASP.Net Repeater control with a Table inside it. Is it possible to run a JavaScript function directly AFTER I call MyRepeater.DataBind()? I've been trying different things, but nothing is triggering the JavaScript function.
Thanks
Databinding occurs on the server in a postback as part of the Page Lifecycle process. In other words, excluding partial-postbadks, at the time this happens any existing DOM in the browser is destroyed. A whole new page is constructed on the server and transmitted to the browser, so that a new DOM can be built and rendered.
What all that means is that you want to think in terms of running your javascript in the page's onload event. One way to make this happen is using the ClientScriptManager.
Javascript can be called from server side by using RegisterStartupScript and RegisterClientScriptBlock methods.
http://www.mindfiresolutions.com/Register-clientside-startup-script-from-serverside-code-286.php
No. The javascript isn't even going to render and run until the code-behind has executed and the page delivered to the client. So it won't matter if adding the script is the first thing you do in the code-behind or the last thing you do (or directly after the DataBind()).
When using the ClientScriptManager Class, look at your code behind and you'll see the dynamic javascript is added just before the ending </form> tag (although it still may be possible to accomplish what you want to do, just with a different approach).
Well I found a solution, not sure it's the cleanest way to do it, but for my application's context it works:
I ran the javascript code after a partial postback using: Sys.WebForms.PageRequestManager.getInstance().add_endRequest();
Again, not the cleanest but suits the needs I have.
Thanks for all your input

Categories