I have been trying to embed an applet within a JSP page to call some needed methods. The applet is initialized correctly but the method call does not work.
With HTML, a similar example was working fine, but with JSP the method call does not seem to work.
Here is my JS function:
function myFunc() {
alert('begin');
(document.applets['MyApplet'].getName1());
alert("done");
return true;
}
Here is the JSP part:
<body >
<script src="http://www.java.com/js/deployJava.js"></script>
<button onclick="return myFunc();" >button</button>
<jsp:plugin align="middle" height="500" width="500" type="applet" code="MyClass.class"
name="MyApplet" codebase="."/>
</body>
I have tried using the applet tag and it is not working either.
If someone has any experience with this, it would be very helpful.
Thank you.
Related
I have a page which contains another page embedded inside an object tag.
Now I want to call a javascript function from the "parent"-page. To be specific: The parent page wants to call a function, which resides inside the embedded code.
Former I used iframes but they caused some nasty bugs with Firefox, so I don't want to use them anymore.
Edit:
So, my question is: what would be the best way to achieve this, when using an object tag?
Here's some example to illustrate what I want to do:
I have a HTML page "parent.html" inside this page there is some Javascript inside a tag. This parent.html also has an tag and the src of this tag is another HTML page, let's call it child.html. The child.html page has something like:
Here's some pseudo code:
in Child.html:
<script type="text/javascript" src="child.js"></script>
in Child.js:
function getSomething(){
return something;
}
in Parent.html:
<object id="childObject" data="child.html" width="850" height="510">
</object>
<script>
// Here I want to access the function from the child.js
// I tried something like this, but it doesn't work:
var something = document.getElementById('childObject').contentDocument.getSomething();
</script>
Now:
In the Javascript inside the parent.html I need to call a function from the child.js.
How can I achieve this?
Thank you :)
Using contentWindow instead of contentDocument works for me:
<object id="childObject" data="child.html" width="850" height="510">
</object>
<script>
var something = document.getElementById('childObject').contentWindow.getSomething();
</script>
You Can try This
<object id="childObject" data="child.html" width="850" height="510">
<script>
var something = document.getElementById("childObject").parentElement.getSomething();
</script>
I know this question has been asked many times and I have checked all the solutions and researched everything. However, this is simply not working for me.
I don't know what I am doing wrong. Can someone please help me out?
I am loading a local html file in my WebView and then calling the JavaScript function:
wv.loadUrl("file:///android_asset/sample.html");
wv.getSettings().setJavaScriptEnabled(true);
JavascriptInterface javasriptInterface = new JavascriptInterface(MyActivity.this);
wv.addJavascriptInterface(javasriptInterface, "MyInterface");
wv.loadUrl("javascript:loadpath()");
The HTML file is:
<html>
<head>
</head>
<body>
<script type="text/javascript">
function callDoSomething() {
// Do something
}
function loadpath() {
// Is not called no matter whatever operation I do here. Just printing a string, setting variable, android callback anything.
document.write("Hi");
document.getElementById('img').src = "path.png";
}
</script>
<form name="myForm" action="FORM">
<img src="" alt="Autofill" /><br>
<input type="button" value="Submit" onClick="callDoSomething()" />
</form>
</body>
</html>
loadUrl() is asynchronous. You are calling your second loadUrl() way too soon. You need to wait until your page is loaded, perhaps by using a WebViewClient and watching for onPageFinished().
I'm a beginner trying my first program to add external jscript file in scr attribute of script tag, followed all steps as I searched but it's not working the way it should. Can someone please help me with this?
I have one aspx form, and one button onclick calling internal javascript function.
I also have one button onclick calling external .js file function.
This is my aspx code
<head runat="server">
<script type="text/javascript" language="javascript" src="ExternalJScript.js">
function Myfunction()
{
document.getElementById("htmlbutton").innerHTML = "This is Button from Javascript function";
alert("Hi Function Called from Javascript");
}
</script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<button type="button" id="htmlbutton" onclick="Myfunction()">This is html button</button><br />
<button type="button" id="Button1" onclick="ExternalJSFileFunction()" value="Call File">HI</button>
</div>
</form>
And this is my .js code
ExternalJSFileFunction()
{
alert("I m from external file");
}
There should not be code in between the script tags of an external script. Try changing it to:
<head runat="server">
<script type="text/javascript" language="javascript" src="ExternalJScript.js"></script>
<script type="text/javascript">
function Myfunction()
{
document.getElementById("htmlbutton").innerHTML = "This is Button from Javascript function";
alert("Hi Function Called from Javascript");
}
</script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<button type="button" id="htmlbutton" onclick="Myfunction()">This is html button</button><br />
<button type="button" id="Button1" onclick="ExternalJSFileFunction()" value="Call File">HI</button>
</div>
</form>
Also, the Language attribute is deprecated and is not needed
Edit
It's because the function you are trying to call isn't actually a function because the function keyword is not used. Change the external file so that it is:
function ExternalJSFileFunction()
{
alert("I m from external file");
}
Then it will work
Additionally, there are some other tips as well:
If you're using the HTML5 doctype, you can also get rid of the type attribute on <script> elements too
Also have your opening curly braces on the same line as the function or conditional, so do:
function ExternalJSFileFunction() {
but not:
function ExternalJSFileFunction()
{
You should almost always add your scripts to the end of the page, just before the closing </body> tag for performance
Using the onclick attribute is also not the recommended way of attaching event handlers, you should use the proper addEventListener() method instead. If you need to support <= IE8 you'll need to use IE's older event API. Using a JS library. like jQuery, can really help out with this kind of stuff.
The function in your external JavaScript file is not defined properly.
It should look like this (I added the function keyword).
function ExternalJSFileFunction()
{
alert("I m from external file");
}
You also need to make the changes that danwellman suggested in his answer.
I am just getting started with HTML/JavaScript, and I have a simple question. I am attempting to call a js function from a separate script source, but am having a bit of trouble. My function script (date_button_function.js) reads:
function displayDate()
{
document.getElementById("date").innerHTML=Date();
}
In order to call on this function, my code looks like this:
<html>
<head>
<script type="text/javascript" src="date_button_functoin.js"></script>
<body>
<h1>Testing the Date</h1>
<p id="date">Click below to see the date.</p>
<button type="button" onclick="displayDate()">Display Date</button>
</body>
</html>
When I actually write out the displayDate function in the HTML script, it runs just fine. However, when calling the function, it does not work. If someone could let me know my syntax error, that would be great.
You're not closing your head tag, that's probably your issue there. Also, as stated on the comments, the name of the js file is wrong, should read "date_button_function.js" instead of "date_button_functoin.js"
I have a xbap application and i would like a javascript button to call a function from this application.
I've read something on Internet and it seems i have to use either a webcontrol (is that correct?) or an object control whom to pass an id or something. How should i do that exactly? A small Vb.Net example would be useful.
Let's assume i have this class:
Class Page1
Public Sub Callback()
MsgBox("Something")
End Sub
End Class
And i have a html file with an iframe that looks like this:
<html>
</head>
<script>
function something() {
var ctrl = document.getElementById("testControl");
ctrl.Callback();
}
</script>
</head>
<body>
<object id="testControl" name="testControl" classid="clsid: ..." width="0" height="0"></object>
<input type="button" value="Change Size" onclick="something()" />
<br/>
<script>
document.write('<iframe id="frame1" name="frame1" width="200" height="197" src="http:// .... .xbap?id=' + 1 + '" frameborder="1" border="1" style="border: 1px solid #000000;" />');
</script>
</body>
</html>
How should i configure my vb.net class and html file so that it works? thank you.
This question seems to be unanswered, and is most likely bit duplicate with other questions out there.
But any how, I have created a blog post where there is a javascript button that calls function from xbap. This solution works with latest browsers out there.
Check it out at:
http://hotzblog.com/xbap-and-javascript-with-ie9-or-newer-solution-for-missingmethodexception-problems/