How to avoid hardcoding full URL of an xml file? - actionscript-3

I am bound to provide a full URL : www.abc.com/folder1/folder2/my.xml rathar than just giving relative URL "./my.xml" .
How can i solve this ? This forces me to hardcode the URL inside the .as file .

I can't understand what you actually need: relative or absolute URL?
If relative, use it. Just consider that it should be relevant to the HTML wrapper, not the SWF itself.
If absolute and you just do not want to embed it into the code, pass it to your SWF from the HTML wrapper using FlashVars.

You can use a relative url in the flash, and the fact that it's working when you test locally indicates a problem with the embed. My guess is that you have placed the swf in a subfolder from the html page but not specified a base url when embedding the swf. Is the swf file located in www.abc.com/folder1/folder2/ or any other folder other than the html itself?
If so, you need to tell the flash player which url all relative url:s in the flash should use as a starting point. This is done using the "base" parameter. If you embed the swf using SWFObject you specify it with an object with a base property:
var flashvars = {
};
var params = {
base: "/folder1/folder2/"
};
var attributes = {
};
swfobject.embedSWF("myContent.swf", "myContent", "300", "120", "9.0.0","expressInstall.swf", flashvars, params, attributes);
Note that I assumed that the .swf is in folder2/ in the example above.
Hope this solves your problem.

Related

toDataURL Image download has no extension

I am downloading an image when a div is clicked by using...
document.location.href = save_canvas.toDataURL("image/jpeg").replace("image/jpeg", "image/octet-stream");
This is working but the image that downloads has no extension and is just called 'download'
I have tried setting the name like this...
document.location.download = "myfile.jpg";
document.location.href = save_canvas.toDataURL("image/jpeg").replace("image/jpeg", "image/octet-stream");
But it is having no effect, where am i going wrong?
The download attribute is not part of the Location object as document.location holds, only for the HTML anchor (A) tags (except in IE).
Depending on browser and version you could instead try to convert canvas into a Blob object, then to File in order to set a filename, and serve that as URL via URL.createObjectURL(). Also here, toBlob() is not supported in IE (but you can polyfill toBlob(), and use msSaveBlob instead).
(and you would also want to replace mime-type's "image" with "application" for mime-type (e.g. "application/octet-stream"). )
c.toBlob(function(blob) {
var file = new File([blob], "test.png", {type: "application/octet-stream"});
document.location.href = URL.createObjectURL(file);
})
A save request with PNG and filename should appear when running this code...
<canvas id=c></canvas>
Optionally, try the FileSaver.js library which deals with many special cases.

Grails 2.5 cannot locate image path in js file

I'm using asset pipeline in Grails 2.5.x. I'm put the img folder which contains images into asset folder.
However in js file, I cannot use <assets:image tag in the image path.
For example:
controlHTML: '<img src="../img/up.png" style="width:40px; height:40px" />'
I can get to the path but in web inspect element, the path is incorrect: 'localhost:9090/CardReg/img/up.png'
Error when I put
controlHTML: '<img src="<asset:image src='../img/up.png'/>" style="width:40px; height:40px" />'
Any ideas?
Any solutions will be appreciated. Thanks you.
Grails tags are not usable from within a .js context. Only from within a .gsp
I would recommend putting
<asset:script>
var contextPath = "${request.contextPath}";
</asset:script>
In your main gsp layout file, and then from your js you can reference images via
var myImage = contextPath + "/assets/myImage.jpg"
I also recommend practicing smart javascript, so you might want to put any of these layout variables declared into an object to act as a namespace.
Also make sure that the script block is the very first script imported.

How to use ajax to set the src of an image?

I need to set some headers when getting an image. The img src attribute does not allow this, so I'm using an XHR request to get the image. However, when I set the src attribute on the img tag after that request completes, it looks like the request is triggered again. Is there a way to cache the image and not trigger the second request?
Sample Code:
$(document).ready(function() {
var url = 'https://i.imgur.com/bTaDhpy.jpg'
var file = $.get(url);
file.then(function(data) {
$('#foo').attr('src', url);
});
});
JS Fiddle: https://jsfiddle.net/mehulkar/o4Lcs5Lo/
Note: my question is not about how to set the appropriate headers in the xhr request. My question is how to not trigger another GET from the setting of the src attribute and use the response from the XHR to display the image.
Use the $.ajax for this:
var myImg = $('#foo'),
mySrc = 'https://i.imgur.com/bTaDhpy.jpg';
$.ajax({
url: mySrc,
type: "GET",
headers: {
"X-TOKEN": 'xxxxx'
}
}).done(function() {
myImg.attr('src', mySrc); // set the image source
}).fail(function() {
myImg.hide(); // or something other
});
I cannot comment here due to reputation points, but here is what I've found on this.
I have a local html page that I'm executing via file:///
I can use a $.get to dynamically pull svg files into a variable. The debugger shows this as if it were a standard html node <svg. (I'm using Firefox Developer, but also in Firebug I see the svg file as a node.).
So At this point I have an empty <img that I want to set to my svg file I just don't know how to set the src attribute to the actual document itself. I suppose I could encode to base64.. You might be able to set it using the debugger itself. I couldn't get this to work reliably. Another avenue (for me since I'm using svg) is to clone the object then write it node for node. If you're not using svg perhaps there is some similar hack you could conduct with canvas? Load the image as a sprite then read the colors and set pixels?

pass a base path to a swf loaded at runtime

I have a main.swf which loads a module.swf and the module.swf loads some assets.
The module.swf works standalone and also needs to work when loaded by main.swf.
But unfortunately the module.swf can't find the assets when loaded by main.swf because the assets aren't located relative to the main.swf, but are located relative to the module.swf.
As I can't touch the module.swf and I'm also unwilling to change the directory structure, I am looking for a solution close to the a "base" parameter which can be used when a swf is embedded into html.
Is there a way to simulate the base parameter's behaviour when loading a swf file using Loader?
Here is a similar yet unanswered question.
You could use symlinks as a way to redirect your assets url when loaded via the main swf
Something like this
public class URLUtil
{
public static function getBaseURL(swfURL:String):String
{
return swfURL.substr(0, swfURL.lastIndexOf("/") + 1);
}
}
example of usage inside module:
var assetURL:String = URLUtils.getBaseURL(root.loaderInfo.url) + "asset.filename";
loader.load(new URLRequest(assetURL));

Get Current Browser URL - ActionScript 3

I'm trying to get current browser url. I have already tried with External Call, and it didn't work. And with loaderInfo.url I receive the current SWF url.
Give this a go:
import flash.external.ExternalInterface;
var url:String = ExternalInterface.call("window.location.href.toString");
if (url) textfield.text = url;
should do the trick.
There are a couple of ways to solve this problem, however all of them involve the use of JavaScript to query the browser directly.
My preferred way to solve this problem would be to provide the URL via a flashVar property, direct from the embed code (personally, I would reccomend using SWFObject to make this easier); don't forget you will need to URL Encode it to avoid markup issues.
var flashvars = {
browserURL: escape(location.href)
};
swfobject.embedSWF("myContent.swf", "myContent", "300", "120", "9.0.0", "expressInstall.swf", flashvars);
Now you will be able to access the Browser URL via the loaderInfo object:
trace(stage.loaderInfo.parameters["browserURL"]);
note that this will only work if you have control of generated HTML for your SWF file - if users are going to be grabbing the SWF and writing their own embed HTML, it's not going to work.
If you don't have control of the flash embed HTML, then you will need to get flash to query the browser at runtime using the ExternalInterface class; other people have suggested the use of "window.location.href.toString" however this can prove problematic in IE6, I find the following works reliably across all browsers
const browserURL : String = ExternalInterface.call("eval", "window.location.href");
Note that in order for this to work, you will need to grant JavaScript access to your Flash movie, this is done, again, via the HTML embed code and the allowScriptAccess param
var url:String = loaderInfo.loaderURL;
seems to work too.
I would try passing the required info in as a flashvar. Not the best out of the box solution I know, but it will work.
Flash: FlashVars in AS3
i think its posible to use the external interface an do it with javascript window.location
I have been using flash for a long time and never noticed this one. It only gives the domain though for security. It does work through loaded swfs as well. Not sure about iframes.
Security.pageDomain