I'm trying to create simple Internet Explorer 9 addon using .Net and BHO to load external js-file on page and execute it.
I created ieInstance_DownloadComplete (I also tried ieInstance_DocumentComplete, but things were worse) event handler:
InternetExplorer explorer = this.ieInstance;
var document = explorer.Document as IHTMLDocument2;
document.parentWindow.execScript(
#"
if (document.getElementById('KCScript') == null)
{
var fileref=document.createElement('script');
fileref.setAttribute('id', 'KCScript');
fileref.setAttribute('type','text/javascript');
fileref.setAttribute('charset', 'UTF-8')
fileref.setAttribute('src', 'C:/test.js');
fileref.onload = function () { eee(); };
if (typeof fileref!='undefined')
document.getElementsByTagName('head')[0].appendChild(fileref);}","JavaScript");
}
When page is loaded I can see my test.js attached to page in IE Developer Tools. But function eee() does not raise and i have an error:
"SCRIPT1014: Invalid character
test.js, line 1, character 1"
test.js:
function eee()
{
alert('ttt!');
};
test.js is UTF-8, so there is no reading problem.. there is something else
What's wrong? Any ideas?
Thanks in advance!
Try to resave JS in "UTF-8 without BOM" encoding
Try to place JS to remote server as http://example.com/test.js
Related
I'm currently building a game, when I tried uploading it to AirConsole the game gives me a error in the preview.
Has anyone had this problem before?
"Uncaught ReferenceError: AirConsoleViewManager is not defined".
var airconsole = new AirConsole({ "orientation": "landscape", "device_motion": 10 });
var vm = null;
window.onload = start;
airconsole.onReady = function () {
//THE LINE UNDER CRASHES
vm = new AirConsoleViewManager(airconsole);
};
ReferenceError: AirConsoleViewManager is not defined
at AirConsole.airconsole.onReady (https://storage.googleapis.com/XXX.xxxxxxx.xxxx.cdn.airconsole.com/2018-05-24-16-33-25/controller.html:125:22)
at AirConsole.onPostMessage_ (https://www.airconsole.com/api/airconsole-latest.js:1053:8)
at https://www.airconsole.com/api/airconsole-latest.js:969:8
Thank you very much :)
I assume you got the information about AirConsoleViewManager from here (GitHub).
I can't tell for sure because you haven't linked the entire file, but you need to download the airconsole-view-manager.js and include that in your controller script.
The example from the previously linked GitHub example is kind of weird because it doesn't include the JS file in it. Most likely the reason for this is that they assume (which you should never do as a developer) people know it already.
EDIT: Actually, they do include the file but it's not done in JS file, it's in HTML file:
<script type="text/javascript" src="airconsole-view-manager.js"></script>
I do not know what the problem was, but after cleaning the build and transfering code over to another project, then uploading again it worked!
This question already has answers here:
How can I get the WebBrowser control to show modern contents?
(4 answers)
Closed 6 years ago.
My webbrowser control displays an intranet site. It was working fine, until the admin changed a setting in iis that forces ie11 to render in Edge mode. Now my webbrowser control comes up with the script error "object doesn't support property or method attachEvent."
Yes, I know attachEvent is deprecated in ie11. No, I do not have control over the webpage code. No, I can't force the admin to change the setting back again.
I tried using registry settings for my application under browser emulation, using all the codes starting with ie9 up through ie10. None of them had any effect.
Can anyone tell me how to force my webbrowser control to render in such a way as to avoid that script error and continue logging in? The call to attachEvent must be called upon successful login, because when I used bad credentials on the login page the error doesn't come up. When my application was working, the page defaulted to ie 9 compatibility. But it seems the admin's IIS setting has nullified that.
Since you cannot alter the code, I recommend implementing a attachEvent/detachEvent polyfill, like this
HTMLElement.prototype.attachEvent = function(event, cb) {
var onEventName = "on" + event,
obj = this;
if (obj.addEventListener) {
obj.addEventListener(event, cb, false);
} else if (obj.attachEvent) {
obj.attachEvent(onEventName, cb);
} else {
var currentEventHandler = obj[onEventName];
obj[onEventName] = function() {
if (typeof currentEventHandler === 'function') {
currentEventHandler.apply(obj, arguments);
}
cb.apply(obj, arguments);
};
}
};
HTMLElement.prototype.detachEvent = function(event, cb) {
var onEventName = "on" + event,
obj = this;
if (obj.removeEventListener) {
obj.removeEventListener(event, cb, false);
} else if (obj.detachEvent) {
obj.detachEvent(onEventName, cb);
} else {
delete obj[onEventName];
}
};
Here's a working plnkr demonstration (apologies for not using a snippet). I based this code using this old git.
Do note the code is incomplete and not for production, e.g. it is not checking whether HTMLElement, attachEvent, detachEvent exist.
I'm setting up my jQuery bundle as follows:
var bundle = new ScriptBundle("~/bundles/jquery", "//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js");
// Path to the version of the file on your server (in case the CDN fails)
bundle.Include("~/scripts/jquery-{version}.js");
// JS expression to run, to test if CDN delivered the file or not
bundle.CdnFallbackExpression = "window.$";
bundles.Add(bundle);
In order to increase my Google PageSpeed Insights score I chose to load the script asynchronously:
#Scripts.RenderFormat(#"<script src=""{0}"" async></script>", "~/bundles/jquery")
But now, it seems to always fail the CDN fallback expression and ends up loading the script twice; once from my fallback bundle and once from the CDN when the async call finishes. I assume because when it runs the fallback test, the async call to the CDN hasn't finished.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js" async=""></script>
<script>(window.$)||document.write('<script src="/bundles/jquery"><\/script>');</script>
<script src="/bundles/jquery"></script>
Is there a more intelligent way I can write my fallback expression?
Is there a better solution entirely?
I'd load jQuery closer to the bottom but then it'd break any inline calls that depend on it.
<script id="srequire" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.jsWRONG" async="async"></script>
<script type="text/javascript">
function sOnerror() {
console.log("sOnerror");
//if (typeof window.jQuery === 'undefined') {
var scr = document.createElement("script");
scr.src = "/bundles/jquery";
scr.async = true;
document.body.appendChild(scr);
//}
}
var element = document.getElementById("srequire");
console.log("element", element);
if (element.addEventListener) {
//element.addEventListener("load", onsuccess, false);
element.addEventListener("error", sOnerror, false);
} else if (element.attachEvent) {
//element.attachEvent("onload", onsuccess);
element.attachEvent("onerror", sOnerror);
} else {
//element.onload = onsuccess;
element.onerror = sOnerror;
}
</script>
for u to test it I put WRONG in the js name to check that the script works
I checked it on ff chrome ie opra and edge
you can use requirejs but that also fail sometimes from cdn
that's why I use this script
happy to share hope it helps
"If the load resulted in an error (for example a DNS error, or an HTTP 404 error)
Executing the script block must just consist of firing a simple event named error at the element."
source w3.org
I have built a BHO (Browser Helper Object) in C# which detects phone numbers in web pages and places an image with a hyperlink next to it. The BHO basically inserts a javascript which uses a Regex String to find phone numbers and adds the image next to it.
This is the relevant code
public void OnDocumentComplete(object pDisp, ref object URL)
{
HTMLDocument document = (HTMLDocument)webBrowser.Document;
IHTMLElement head = (IHTMLElement)((IHTMLElementCollection)
document.all.tags("head")).item(null, 0);
IHTMLScriptElement scriptObject =
(IHTMLScriptElement)document.createElement("script");
scriptObject.src = "\nhttp://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js";
((HTMLHeadElement)head).appendChild((IHTMLDOMNode)scriptObject);
IHTMLScriptElement scriptObject2 =
(IHTMLScriptElement)document.createElement("script");
scriptObject2.text = "\nwindow.onload = function()"+
"{"+
"$('body').html( $('body').html().replace(/(\\d\\d\\d\\d\\s\\d\\d\\d\\s\\d\\d\\d)/g,'$1 <img src=\"image.png\" border=\"0\">') );"
+"}"+
"\n\n";
((HTMLHeadElement)head).appendChild((IHTMLDOMNode)scriptObject2);
}
I have tested the BHO in IE on a very simple page with few phone numbers. It works as expected. But when i test the BHO with any other page on the web i get the following error
Microsoft JScript runtime error: Object doesn't support this property or method
or
Microsoft JScript runtime error: Permission denied
I am getting Microsoft JScript runtime error for a few other pages. Does this mean that i am not allowed to insert a javascript into the page ? What could be the reason ? I hope this is the right way to do it.
I think the errors are thrown because you add jQuery multiple times to the site you're loading. Have you ever tried to set a breakpoint inside the OnDocumentComplete method?
If you do so, you will see, that the OnDocumentComplete event is fired more than one time, per site request.
So you should check at first, if it's the first time the OnDocumentComplete event is fired for the actual site request. If so, add you javascript, if not, do nothing.
This should prevent double jQuery includes.
I am using Visual Studio. My javascript is functioning if I debug it at local host, the function is very simple, photo change at mouseOver.
But after I upload my file to my server, this function is not working. I have tried it on Chrome, Firefox, IE and Safari:
function mouseOver1()
{
var category1 = document
.getElementById('ctl00_ContentPlaceHolder1_lblItemID')
.innerHTML + '_1.jpg';
document
.getElementById('<%= iPhotoMain.ClientID.Replace("$","_") %>')
.src = 'photos/product/' + category1;
}
<img id ="photo1"
src ="photos/product/noImage.gif"
onmouseover="mouseOver1();"
class="thumbpic"
runat ="server" />
In light of comments made by the OP, my original answer is partially incorrect (although I still don't think you should be using static ID for the lblItemID - so will leave the answer below).
My new guess would be that you have a second mouseOver1(); function defined somewhere on your page, or more likely an external JavaScript file that you are linking to. So I would recommend that you check all external JavaScript files on your server that you link to and make sure they're as you would expect.
I can recommend firebug for firefox or the developers tools under IE and Chrome (F12 on both). With firebug you can breakpoint on the next Javascript to fire, so you can easily find out where the call is going to
Original Answer
My guess is that you have mixed up your object ID's. You are also using a static string to find one of your objects.
If I am correct, please try this - if I am incorrect in my guess, I will happily delete my answer.
(Note, I have changed iPhotoMain into photo1)
function mouseOver1(){
var category1 = document
.getElementById('<%=lblItemID.ClientID%>')
.innerHTML + '_1.jpg';
document
.getElementById('<%=photo1.ClientID%>')
.src = 'photos/product/' + category1;
}
Maybe on server your pages(data) coming slower than your local. So try callback functions with javascript. Like this..
var category1 = "";
function mouseOver1(callback)
{
category1 = document
.getElementById('ctl00_ContentPlaceHolder1_lblItemID')
.innerHTML + '_1.jpg';
callback();
}
function mouseOver2()
{
document
.getElementById('<%= iPhotoMain.ClientID.Replace("$","_") %>')
.src = 'photos/product/' + category1;
}
<img id ="photo1" src ="photos/product/noImage.gif" onmouseover="mouseOver1(mouseOver2);" class="thumbpic" runat ="server" />