2

I'm struggling with an encoding-problem in a small system I'm constructing.

In an HTML, this script is loaded

<script src="http://localhost:8000/serving/dk-711218"></script> 

and normally I can't access the HTML so everything has to be done inside the javascript file.

The server-side scripts are made in Node.js and it returns pieces of code depending on some settings in customizable XML files. For instance, when displaying an image the system returns a simple

<img src="mypicture.jpg" />

and if it's a text, it returns

<div class="myClass">This is a test</div>

and if they have special behaviors, this code is included as well.

These parts work as intended. These chunks of code resides inside a number of classes and are returned as needed so that the code is gradually built.

So far, so good.

The problem is returning the SWFobject library code, because it seems to corrupt the code on the fly.

All code has been escaped and encoded with encodeURIComponent so that the system just needs to decode and unescape. But the validation fails.

Here's an example of the first few lines before encoding/escaping:

var%2520swfobject%253Dfunction%2528%2529%257Bvar...

Here's how a piece of the SWFObject looks like in the Firefox source code window when accessing the page:

and here's how a piece of the decoded SWFObject looks like in the same window:

This occurs at several places and something that's common for these occurrences is that it looks like the less-than character for unknown reasons is interpreted differently.

Here's the view renderer and I can't figure out if problem is caused in the code or when rendering the code.

Any ideas to what's causing this behavior? Or perhaps some advices on best practice when including code this way?


Responses to comments:

try JSON.stringify

I've tried the JSON solution out as well and it does the trick! What I did was - as before - to pre-process the included code, using a little tool I built with two input-fields and a JSON.stringify-command between the two. This resulted in the content of returnvar:

Module.prototype.opens = function () {
    var returnvar = "var swfobject=function(){var D=\"undefined\",r=\"object\",S=\"Shockwave Flash\",W=\"ShockwaveFlash.ShockwaveFlash\",q=\"application/x-shockwave-flash\",R=\"SWFObjectExprInst\"... etc. 

and a JSON.parse is used to correct it again in the renderer:

router.get('/serving/:id', function (req, res) {
    var readSymbolicsXMLCallback = function(data) {
        res.render('index', {
            id: req.params.id,
            embedcode: JSON.parse(data)
        });
    }
    var embedcode = readSymbolicsXML(req.params.id, readSymbolicsXMLCallback);
});
Anders
  • 251
  • 3
  • 13
  • `unescape` is deprecated, and according the MDN (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/unescape) it is in the process of being dropped. So that method could disappear at any time. Both `unescape` and `decodeURIComponent` are for handling strings that are to be placed in a URL... so unless if your putting your code inside a URI expected place, this doesn't make any sense to me... and looks like it might be the source of your problems – Norman Breau Jun 12 '15 at 02:59
  • Hi Breau and thank you for answering. So I should perhaps do a manually backslash escaping and search/replace when rendering? – Anders Jun 12 '15 at 04:04
  • Laggingreflex, please see the edited question – Anders Jun 12 '15 at 08:55
  • 1
    If your `returnvar` is already JSON encoded string, just send that, no need to stringify again. Then on the *client side* `JSON.parse` it – laggingreflex Jun 12 '15 at 13:57
  • @laggingreflex Yes, I made a little converter in order to pre-JSON'ify the SWFobject-file - and then, in the renderer to parse the JSON to the view - and it might work now. I've included an alert-box, which outputs the swfobject-variable and it displays [object Object]. I don't see that any flash-file is loaded, according to the console - but it's probably another matter. One thing that puzzles me, though: the webbrowser apparently treats two identical codes differently: [Code comparison](http://www.vandret.dk/diverse/comparison.gif) – Anders Jun 12 '15 at 23:21
  • Another thing... please disregard from the URI-error, mentioned above - I should, of course, not run JSON-stringified code through the decodeURIComponent - leftover from earlier. – Anders Jun 12 '15 at 23:23
  • @laggingreflex Never mind, NOW IT WORKS!!! You are a hero. Please post as an answer and you'll get your merits :) – Anders Jun 12 '15 at 23:53
  • 1
    @Anders I'm actually still a little hazy on the exact details of your setup, and so my answer would only be my short comment above. The best kind of answer would include both the server-side code (how you sent the JSON.stringified code) and client-side code (how you parse it and use it in JS) both showing exactly how the setup worked out for you. And possibly showing why/how exactly did the other method not work. So for that reason I think only you would be in a better position to post a detailed answer. I'm just glad it worked for you :) – laggingreflex Jun 13 '15 at 06:54

0 Answers0