0

I'm having a problem with the following function. It's rendering in the browser as [object Window] instead of the HTML content...

BTW, the eval(func_fix); is executing InsertMenu() correctly.

Any idea why?

Thank you in advance.

<script>
$(document).ready(function() {
    $("#component").each(function() {
        var func = $(this).attr("name");
        var func_fix = func.replace(")",","+$(this).attr("data-pagenum")+")");

        var content = eval(func_fix);
        $(this).replaceWith("<div>" + content + "</div>");
    });
});

function InsertMenu(param,pagenum) {
$.get("compmenustructure.asp",{pagenum: pagenum, stylemenunum: param})
.done(function(data) {
    content = data;
});

return content;

}
</script>
srecce
  • 97
  • 3
  • 15

1 Answers1

0

You should read JavaScript (that is, ECMAScript 5.1) reference better.

"<div>" + content + "</div>" is + operation between string and an object, and in that case, an object is converted to its string representation by calling its .toString() method.

So, content contains the window object. Make it contain the string you want to include in div (I don't know what do you want to have there, your code is just part of page; no one can answer you unless you say what is in name and data-pagenum attributes, for example).

  • name = InsertMenu(7) and data-pagenum=38 I'm just tried to include a menu using ajax. If I add an alert(content) before $(this).replaceWith("
    " + content + "
    "); it renders fine... but I dont want to output this alert...
    – srecce Jan 27 '14 at 17:02
  • You should add alert with "
    "+... and you will see it's not correct. Not to mention the *BIG* bug: you cannot just "return content" from ajax call - it is asynchronous. You must do _everything_ (including `replaceWith`) in the `done` callback.
    –  Jan 27 '14 at 17:33
  • (and you use global variable; please put `var content` at the beginning of `insertMenu` function - though you will not need it if you do it right, that is, inside `done`, you can simple use `data`, but anyway - be careful about using globals, it's bad thing) –  Jan 27 '14 at 17:36