Can't get Javascript to interact with Flash - javascript

I can't get my Javascript to affect my SWF object.
The object is rendered correctly in the browser but I cannot call the "fill" method that I exposed to the ExternalInterface.
The ready function "flashReady" is not called. Directly calling it from the console says that the flash object does not have the function "fill". Waiting for a while then calling the function does not help.
The error is TypeError: Object HTMLObjectElement has no method 'fill'
Actionscript code:
public class Main extends Sprite
{
public function Main() {
graphics.beginFill(0xff0000);
graphics.drawRect(0, 0, 200, 100);
graphics.endFill();
Security.allowDomain("*");
ExternalInterface.addCallback("fill", fill);
ExternalInterface.call("flashReady");
}
public function fill():void {
graphics.beginFill(0x00ff00);
graphics.drawRect(0, 0, 200, 100);
graphics.endFill();
}
}
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>
Flash UI Demo
</title>
<script type="text/javascript" src="swfobject.js">
</script>
<script type="text/javascript">
function flashReady() {
document.getElementById('AddCallbackExample').fill();
}
var swfVersionStr = "0";
var xiSwfUrlStr = "";
var flashvars = {};
var params = {};
params.quality = "high";
params.bgcolor = "#ffffff";
params.allowscriptaccess = "always";
var attributes = {};
attributes.id = "AddCallbackExample";
attributes.name = "AddCallbackExample";
attributes.align = "middle";
swfobject.embedSWF(
"http://localhost:8001/swf", "flash",
"100%", "100%",
swfVersionStr, xiSwfUrlStr,
flashvars, params, attributes);
</script>
</head>
<body>
<div id="flash" />
</body>
</html>

Ok I figured it out, my version of the library didn't support allowDomain to take in "*" as an argument. I replaced it with "localhost" and it worked.

Related

everytime i try to print pdf using this method it will always produce blank pdf

so i want to print pdf using button and javascript but it will always produce blank pdf/empty pdf. i want something like https://imgur.com/a/PsRqeBR, but i got this instead https://imgur.com/a/J7rzlSq
i try using window.open and then window.print but it still produce blank pdf, i also tried to put url inside window.open instead of declaring it first but that didn't work
HTML
<button type="button" onclick="printme()">click me...</button>
JavaScript
function printme(){
var URL = "https://www.detik.com/";
var W = window.open(URL);
W.window.print();
}
you need to do window.print() instead of w.window.print()
function printme(){
var URL = "https://www.detik.com/";
var W = window.open(URL);
window.print();
}
The source of the following code is Print an external page without opening it
which was mentioned in one of the answers here Printing a web page using just url and without opening new window?
I only modified the link to include your website address:
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>MDN Example</title>
<script type="text/javascript">
function closePrint() {
document.body.removeChild(this.__container__);
}
function setPrint() {
this.contentWindow.__container__ = this;
this.contentWindow.onbeforeunload = closePrint;
this.contentWindow.onafterprint = closePrint;
this.contentWindow.focus(); // Required for IE
this.contentWindow.print();
}
function printPage(sURL) {
var oHiddFrame = document.createElement("iframe");
oHiddFrame.onload = setPrint;
oHiddFrame.style.position = "fixed";
oHiddFrame.style.right = "0";
oHiddFrame.style.bottom = "0";
oHiddFrame.style.width = "0";
oHiddFrame.style.height = "0";
oHiddFrame.style.border = "0";
oHiddFrame.src = sURL;
document.body.appendChild(oHiddFrame);
}
</script>
</head>
<body>
<p><span onclick="printPage('https://www.detik.com');" style="cursor:pointer;text-decoration:underline;color:#0000ff;">Print external page!</span></p>
</body>
</html>

How can I access a variable in Jquery and use its value in another external .js file ? The variable is $generatedP

I need to access in a different .js file the value inside $generatedP and display it
$(document).ready(function() {
var $buttonValue = $(".value_generate");
var $divValue = $(".generated_value");
var $generatedP = $(".generated_p");
var $valueInput2 = $(".value_input_2");
var $submitPages2 = $(".submit_pages_2");
function valueGenerator(value) {
var valueString="";
var lettersNumbers = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for(var i = 0; i < value; i++)
valueString += lettersNumbers.charAt(Math.floor(Math.random()* lettersNumbers.length));
return valueString;
}//generate string
$buttonValue.click(function generate() {
var $key = valueGenerator(12);
$generatedP.html($key);//display generated string
});
$submitPages2.click(function() {
if($valueInput2.val() == $generatedP.text() ){
alert("you are logged in website");
} else {
alert("please check again the value");
return false;
}//check value if true/false
});
I am new to jquery
You have a few options.
Create a namespace inside the jQuery object:
$.myGlobalNamespace = {};
$.myGlobalNamespace.generatedPvalue = "something";
Define an object at the window level:
window.myGlobalNamespace = {};
window.myGlobalNamespace.generatedPvalue = "something";
Just be sure to use a sensible name for the namespace object.
You can improve the behavior doing client-side checking with localStorage, or you can simply use sessionStorage. Variable $generatedP will be available in page1 and page2. Hope it helps!
PAGE 1:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<script type = "text/javascript">
$(document).ready(function(){
var $generatedP = "27.23.10";
sessionStorage.setItem('myVar', $generatedP);
window.location.href = "page2.html";
});
</script>
</body>
</html>
PAGE 2: to access the variable just use the getItem method and that is all.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
var data = sessionStorage.getItem('myVar');
alert(data);
</script>
</body>
</html>

More questions about Javascript classes

I have tested quite a bit about making classes, now I finally discovered how it works, that's what I want to make class simple and clear, without spamming 'this'
The result is like that, now I only have 2 questions:
redsheep.options.show().sizes, is to call out this variable, now I think it is too long. How do I make it like redsheep.sizes?
redsheep.options.show().eat is not inherit from the sheepclass grass, and became undefined. How do I make default values for newly created objects?
<html>
<head>
<meta charset='utf-8'>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<title>A simple javascript function</title>
<script type="text/javascript">
$(document).ready(function(){
var sheepclass = function(options){
this.options = {'sizes' : 'thin',
'eat' : 'grass',
'color' : 'white',
show : function(){
return {'color':options.color,
'eat':options.eat,
'sizes':options.sizes
};
}
}
}
var blacksheep = new sheepclass({'color':'black'});
var redsheep = new sheepclass({'color':'red','sizes':'fat'});
$('div').append('<p>blackcsheep color : ' +
blacksheep.options.show().color);
$('div').append('<p>redsheep color : ' +
redsheep.options.show().color +
'<p>redsheep size:' +
redsheep.options.show().sizes +
'<p> redsheep eat:' +
redsheep.options.show().eat);
})
</script>
<style>
div{display:block;width:800px;height:400px;border:2px solid red;}
</style>
</head>
<body>
<div>
Result:
</div>
</body>
</html>
Adding a method to an object that returns its own properties is useless. Remove that and access the properties normally:
var sheepclass = function(options) {
this.options = $.extend({
'sizes': 'thin',
'eat': 'grass',
'color': 'white',
}, options);
}
You can access it like this:
redsheep.options.eat
Something like that would do:
var sheepclass = function(options) {
this.sizes = options.sizes || 'thin';
this.eat = options.eat || 'grass';
this.color = options.color || 'white';
}
That way, you can access properties directly (by using for example blacksheep.sizes):
var blacksheep = new sheepclass({'color':'black'});
console.log(blacksheep.sizes); // prints 'thin'

Load external SWF with parameters in AS3

I have a swf file created by EasyPano tourweaver software. the outpout is a swf file with some .bin files to config the swf and other files such as .jpg, .js and so on.
The software create a html file to add the swf but i have to load the swf using flash and AS3. the HTML and JavaScript that the software create is :
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Mahan</title>
</head>
<body leftMargin="0" topMargin="0" rightMargin="0" bottomMargin="0">
<script type="text/javascript" src="swfobject.js"></script>
<div id="flashcontent">
To view virtual tour properly, Flash Player 9.0.28 or later version is needed.
Please download the latest version of Flash Player and install it on your computer.
</div>
<script type="text/javascript">
// <![CDATA[
var so = new SWFObject("twviewer.swf", "sotester", "100%", "100%", "9.0.0", "#000000");
so.addParam("allowNetworking", "all");
so.addParam("allowScriptAccess", "always");
so.addParam("allowFullScreen", "true");
so.addParam("scale", "noscale");
//<!-%% Share Mode %%->
so.addVariable("lwImg", "resources/talarmahan_1_firstpage.jpg");
so.addVariable("lwBgColor", "255,255,255,255");
so.addVariable("lwBarBgColor", "255,232,232,232");
so.addVariable("lwBarColor", "255,153,102,153");
so.addVariable("lwBarBounds", "-156,172,304,8");
so.addVariable("lwlocation", "4");
so.addVariable("lwShowLoadingPercent", "false");
so.addVariable("lwTextColor", "255,0,0,204");
so.addVariable("iniFile", "config_TalarMahan.bin");
so.addVariable("progressType", "0");
so.addVariable("swfFile", "");
so.addVariable("href", location.href);
so.write("flashcontent");
// ]]>
</script>
</body>
</html>
Please Help me!
Thanks
The answer is URLVariables passed to the URLRequest feed into load method of Loader:)
example:
var loader:Loader = new Loader();
var flashvars:URLVariables = new URLVariables()
flashvars["lwImg"] = "resources/talarmahan_1_firstpage.jpg";
flashvars["lwBgColor"] = "255,255,255,255";
flashvars["lwBarBgColor"] = "255,232,232,232";
flashvars["lwBarColor"] = "255,153,102,153";
flashvars["lwBarBounds"] = "-156,172,304,8";
flashvars["lwlocation"] = "4";
flashvars["lwShowLoadingPercent"] = "false";
flashvars["lwTextColor"] = "255,0,0,204";
flashvars["iniFile"] = "config_TalarMahan.bin";
flashvars["progressType"] = "0";
flashvars["swfFile"] = "";
flashvars["href"] = this.loaderInfo.url;
var request:URLRequest = new URLRequest("twviewer.swf");
request.data = flashvars;
loader.load(request);
addChild(loader);
also with following helper method you can get main SWF parameters (from it's html wrapper) and pass it to the loaded SWF:
public function getFlashVars(li:LoaderInfo):URLVariables
{
var vars:URLVariables = new URLVariables();
try
{
var params:Object = li.parameters;
var key:String;
for(key in params)
{
vars[key] = String(params[key]);
}
}
catch(e:Error)
{
}
return vars;
}
then
var loader:Loader = new Loader();
var request:URLRequest = new URLRequest("twviewer.swf");
request.data = getFlashVars(this.loaderInfo);
loader.load(request);
addChild(loader);
For SecurityError: Error#2000 and here - there are many reasons behind this error

JavaScript Not working SWFObject is undefined

I am getting this error "Microsoft JScript runtime error: 'SWFObject' is undefined"
my code looks like this
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>
<div id="flashcontent">This text is replaced by the Flash movie. </div>
<script type="text/javascript">
var rndPick = 2;
var rndPick = Math.floor(Math.random() * 16) + 1;
var movie = "/Flash/sam" + rndPick + ".swf";
var so = new SWFObject(movie, "mymovie", "955", "170", "8", "#336699");
so.write("flashcontent");
setTimeout("location.reload(true);", 14500);
</script>
You're using SWFObject 1.5 syntax, but linking to the SWFObject 2.2 JS file. SWFObject 1.5 and 2.2 are incompatible.
Rewrite your SWFObject code to use the 2.2 syntax. Here is your code converted to SWFObject 2.2 syntax. Note that swfobject.embedSWF is automatically executed when the DOM has finished loading.
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>
<script type="text/javascript">
var rndPick = Math.floor(Math.random() * 16) + 1;
var movie = "/Flash/sam" + rndPick + ".swf";
var flashvars = {}; //empty for this example
var params = { bgcolor: "#336699" }; //sets background color
var attributes = { id: "mymovie" }; //sets ID of <object> to "mymovie"
//Optional callback function gets executed after <object> is created
var callbackFn = function (){
setTimeout("location.reload(true);", 14500);
};
swfobject.embedSWF(movie, "flashcontent", "955", "170", "8", false, flashvars, params, attributes, callbackFn);
</script>
</head>
<body>
<div id="flashcontent">This text is replaced by the Flash movie. </div>
</body>

Categories

Resources