Search code examples
javascriptjquerysyntaxumbraco

How to fix JavaScript syntax error using Umbraco?


I'm using Umbraco, trying to follow the Base tutorial as shown on the website.

The following code gives me a syntax error in FireBug:

jQuery("#get").click(function() { 
        var pageId = <umbraco:Item field="pageID" runat="server" />;
          jQuery.get("/base/Sample/GetPageData/" + pageId + ".aspx", function(data){
            alert("Data loaded: " + jQuery(data).test());
          });
    });

I don't know if the umbraco code is causing the error but it is succesfully returning the pageId so the error reads as: Syntax Error: var pageId= 1007

Any ideas where I'm going wrong?

EDIT: Hope this helps clarify things, if not let me know!

Button I am setting up in umbraco template of page:

<asp:Button id="get" Text="getxml" runat="server" />

Although I have just realised that the rendered HTML of the button is a bit of a mess:

<input type="submit" name="ctl00$ctl00$ContentPlaceHolderDefault$get" value="getxml" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$ctl00$ContentPlaceHolderDefault$get&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" id="ContentPlaceHolderDefault_get" />

Why is it doing this??

This is the jquery code as generated in the page:

 jQuery("#get").click(function()
        var pageId = '1082';
          jQuery.get("/base/Sample/GetPageData/" + pageId + ".aspx", function(data){
            alert("Data loaded: " + jQuery(data).test());
          });
    });

I included the enclosing single quotes as suggested but with or without I am getting the same Syntax Error in FireBug and my script is not working


Solution

  • button with the Id 'get' was being generated as ContentPlaceHolderDefault_Get for some reason (Umbraco?). So I changed the name in the get method to the generated name whilst leabing the button Id as get.

    I guess this was a result of me writing the button control in an umbraco template rather than copying over from a .net control