0

I'm using Google App Engine SDK for Python. When I write Javascript functions in a separate .js file and include it in the .py file, I get the following errors in Chrome:-

In .py file:-
Uncaught reference error: initFunc is not defined.

In .js file:-
Uncaught syntax error: Unexpected token <
Resource interpreted as script but transferred with MIME type text/html.

Source codes:-
.py file

print 'Content-Type: text/html'
print ''
print '\
<head>\
    <title>Page</title>\
    <script type="text/javascript" src="script.js">\
    </script>\
</head>\
<body>\
<input type="button" onclick="initFunc();" value="Test" />\
</body>\
'

.js file
function initFunc(){alert("hi");}\

All the errors disappear when I include the initFunc in the .py file itself.

4
  • 2
    Your print syntax is wrong; you need triple quotes or a \ on the first line of your multi-line string. You also have an unquoted <br> in python code, unless that's an attempt to format your code for SO and not actually in your code. You almost certainly want to use a WSGI framework and template system rather than printing HTTP headers and raw HTML, anyway. Commented Jul 11, 2011 at 13:58
  • I had \ in my code. <br> is not a part of my code. Messed it up while trying to format the code here. Corrected these two typos in the code above. Commented Jul 11, 2011 at 14:18
  • Can you please help me debugging the code as written above now. :) Commented Jul 11, 2011 at 14:19
  • what does app.yaml look like? Are you actually serving the .js file in response to a request for it, or are you seeing a 404 in your logs? Commented Jul 11, 2011 at 14:33

1 Answer 1

1

My guess is that you have both main.py and foo.js in the same root directory.
You should map a route for the static contents of your project like javascripts in the app.yaml config file:

1.Create a directory called static/javascripts in the Root of your project

2.Add the javascripts static section in your app.yaml handlers

 -url: /javascripts  
    static_dir: app/static/javascripts

3.Modify your code like this:

print 'Content-Type: text/html'
print ''
print """
<head>
    <title>Page</title>
    <script type="text/javascript" src="/javascripts/script.js">
    </script>
</head>
<body>
<input type="button" onclick="initFunc();" value="Test" />
</body>
"""

As Wooble correctly suggested, you should avoid to code with print and switch to a more fancy Python web-frameworks supported by GAE.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.