This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
calling a Flash ExternalInterface with JavaScript
I have a flash file with AS code. I want to run Javascript that will run a function the AS. For example: In the AS I have a function called "loadXML". The object that holds the SWF file called "pawel" (The ID of the object). How can I run a Javascript code that will run on "pawel" the function "loadXML"? I'm using Flash 6 with AS 3.
You should use ExternalInterface.addCallback() method. See http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/external/ExternalInterface.html#addCallback() for details.
I suggest you check this http://www.redcodelabs.com/2012/04/calling-actionscript-method-from-javascript/ it has running sample and as3/js code.
I believe the goal here is to send the swf (which has compiled an external .as file) the location of an xml file somewhere on the server so the swf can compile, parse, and create something.
I've done this many times, using flash variables, which may be useful to you as well. When embedding the .swf in your webpage you can send it flash variables via js or html, or dynamically with php. Once i have my fla working perfectly with hardcoded variables (like the xml) i then add the flash variable code to the head and make a few minor adjustments to the fla – which usually breaks the fla from running "properly" within flash (because it's now dependent upon these external variables)
anyways, here's how i do my flash vars (there's other ways, worth a good google search)
import flash.net.*;
var flashVars:Object = new Object();
flashVars = this.loaderInfo.parameters;
var xmlVal;
for (var item:String in flashVars)
{
switch (item)
{
case "xmlLocation" :
xmlVal = String(flashVars[item]);
break;
}
}
Here's the javascript which sends the values:
<script type="text/javascript">
//flashObj
var flashvars = {};
flashvars.xmlLocation = "http://google.com/myXML.xml";
var params = {wmode:"transparent"};
var attributes = {};
swfobject.embedSWF("images/banner.swf", "yourSliderId", "175", "300", "9.0.0", false, flashvars, params, attributes);
</script>
this is using SWFObject (an open API flash embedding js library) to handle the swf embed. I prefer it because if you look at the code above you can read it and understand it, the default way is really hard to read, edit and understand.
If you simply need XML and then you're done, this will work for you. If you still need to say hit the 'next' or 'previous' button using javascript, refer to this web site article, i believe this may help you out further:
http://arrixlive.wordpress.com/2005/03/25/javascript-in-love-with-flash-control-swf-from-javascript/
Related
Im using pdfobject along with forcePDFJS which uses pdfjs viewer.js to render pdf's.. Once they are rendered I need to be able to jump to pages without reloading the document.. The documents can be pretty large
I've seen some mentions about using PDFApplicationViewer.pdfview.currentPageNumber. but I haven't seen a good example on how to use it correclty
I've seen two example of using the PDFApplicationViewer
1. PDFApplicationViewer.pdfview.currentPageNumber = pagNum;
2. document.getElementById('mycanvas').contentWindow.PDFApplicationViewer.pdfview.currentPageNumber = pagNum;
Althought the second on make more sense Im not sure where the contentWindow object from the element comes from. Im assuming the pdfobject embeds something that I could get access too but I can't figure it out..
Also, since I couldn't really find alot on this.. Is this even possible..
For time constraint reasons I don't want to have to put together a new viewer using pdfjs.. I like what comes with the viewer.html.. I just need to jump the pages without reloading
PDFObject.embed doesn't return any reference So I looked into the pdfObject code to see how it was embedding the pdf..
for this to work there needs to be a iframe.. So when rendering with pdfobject Im using a configureation as follows:
Notice forcePDFJS=true
if(!pageNum)
pageNum=1;
var options = {
pdfOpenParams: {
view: "FitV",
page: pageNum
},
forcePDFJS: true,
PDFJS_URL: "../pdfjs/web/viewer.html"
};
Here is code that works
var pdfviewer = document.getElementById('pdfviewer');//get the viewer element
var contenttag = pdfviewer.getElementsByTagName("iframe")[0]//got this from pdfobject code
contenttag.contentWindow.PDFViewerApplication.pdfViewer.currentPageNumber = parseInt(pageNum);//change the page
In my case, the pdfviewer id is not available anymore.
PDFObject does return the reference of iframe that it creates. I was using basic HTML + JS so I had to save that reference to global window object to be able to access it from anywhere in the project.
Finally, with the reference we have, we can access the PDFViewerApplication object as below and manipulate the PDF:
I've read through what little documentation there is on Google's Swiffy page, and I don't see any way to pass FlashVars data to the HTML5 swiffyobject.
On an old version of a site there is this embed code (which uses SWFobject) to pass a javascript variable to Flash:
var mainmovie = new SWFObject("mainpage.swf", "themovie", "640", "480", "8", "#000099");
mainmovie.addParam("align", "middle");
mainmovie.addParam("wmode", "opaque");
mainmovie.addVariable("PageLayout", layoutCode);
mainmovie.write("target_div");
In other words, the variable PageLayout is passed to the Flash movie from .js
Is there some way to similarly pass javascript variables to swiffyobjects, either through something similar to SWFobject's addVariable, or through the standard FlashVars interface?
Just answering my own question here because I found the answer.
The Swiffy equivalent of FlashVars is:
stage.setFlashVars("VariableName = value");
where "stage" is the Swiffy HTML5 object.
I have a javascript file that has code with DOM access
var a=document.getElementById("abc").value
I have html file, that contains all DOM information
<html>...<input id="abc" ...></html>
Is there anyway to get C# invoke the javascript file, and return the value of a, back to the c# program?
In reality the JavaScript can be much more complex, and I need to channel those "interested values" back to C#, but let's just consider the simple example mentioning here.
Possible directions I could think is using https://jint.codeplex.com/, or Web browser control. The challenge here is that it not only involves the JavaScript, it also involving the HTML file.
What I want to know is:
Is there a way to channel variable value from JavaScript back to C#?
How to get JavaScript evaluate DOM elements from a HTML file?
Using WebBrowser and Jint:
using (WebBrowser browser = new WebBrowser())
{
browser.ScriptErrorsSuppressed = true;
browser.DocumentText = #"<html><head/><body><input id=""abc"" value=""this is the value in input""></body></html>";
// Wait for control to load page
while (browser.ReadyState != WebBrowserReadyState.Complete)
Application.DoEvents();
dynamic d = (dynamic)browser.Document.DomDocument;//get de activex dom
var jengine = new Jint.Engine();
jengine.SetValue("document", d);
try
{
string val=jengine.Execute(#"var a=document.getElementById('abc').value;").GetValue("a").ToString();
Console.WriteLine(val);
}
catch (Jint.Runtime.JavaScriptException je)
{
Console.WriteLine(je);
}
}
I have a website with a lot of iframes like this:
<iframes src="expamle.com\page.html?var=blabla&id=42" scrolling="no"></iframe>
I have to change var=blabla&id=42 for each iFrame. These parameters are used in the javascript of the iframe. Is there any way to cache(give hints to the browser) page.html (static) once for all variables ?
I have to use an iframe since I want to be able to update this code ( from another server) & to run it in another scope.
No - Anything changing the query string represents a seperate resource for the browser.
However, you may be able to achieve that effect if you can make some slight changes to page.html. If you write it this way:
<iframes src="expamle.com\page.html#var=blabla&id=42" scrolling="no"></iframe>
Note the use of the # character - that's the key there.
The query string becomes simply "page.html" and will cache that way. However, the Javascript of that page will have access to the variable document.location.hash, which will contain "var=blabla&id=42". It'll be written as a single string, but it shouldn't be difficult to parse. Some libraries even use that tag to pass parameters in semi-real-time to iframes for IE6 compatibility.
If it's only used in the javascript but is really only 1 page server side don't use ? But use # it will consider it as the same page but at diferent anchor pounts. So if test.com/#foo is cached then test.col/#bar is too (same page, different anchor points)
You can update the frame URLs from code:
var fr = document.getElementsByTagName('iframe');
var sites = "1.com,2.com".split(",");
for(var x=0;x<fr.length;x++) {
document.getElementsByTagName('iframe')[x].src="http://"+sites[x];
}
I am currently writing a JavaScript script for the Microsoft JScript Runtime Environment. It's not in a browser, but rather going to be run more like a SysAdmin would use VBScript. I've written a lot of code, and while some of it is specific for what needs to be accomplished, a majority of it is supporting framework for the script to do what it needs to do. I'd like to use this sum of code in other future scripting adventures, but as to my current knowledge, I would have to copy and paste these mini-libraries over and over again, and well, that's just an update nightmare for one and two, it's inefficient. I know it's possible to dynamically load JS when I have a window or document, I know it's possible to require() JS files in Node.js, but is this possible in the raw JScript Runtime for MS?
Look into the Windows Script File (*.wsf) format. One of its features is to allow for includes like you're describing. An example taken from the linked documentation:
<job id="IncludeExample">
<script language="JScript" src="FSO.JS"/>
<script language="VBScript">
' Get the free space for drive C.
s = GetFreeSpace("c:")
WScript.Echo s
</script>
</job>
where "FSO.JS" contains the JScript library.
Assuming your talking about the WSH, you can load a file and eval the contents, which is much the same as including it.
var incfso=new ActiveXObject("Scripting.FileSystemObject");
include = function(x) {
eval(incfso.OpenTextFile(x,1).ReadAll());
}
source: http://www.mailsend-online.com/blog/wsh-javascript-includes.html
I couldn't find a global object like "window" for jscript, but you can sort of create one.
var host = this;
var test = "hello-world";
var messagebox = new ActiveXObject("wscript.shell");
if (host.test) {
messagebox.Popup("host.test exists, value = " + host.test);
} else {
messagebox.Popup("host.test does not exist.");
}
I think "host" should now effectively be the global object. (the example works for me anyway)