1

When the gray div is clicked I want the website to be opened in a new tab, but here this javascript code isn't working. Why? How I can edit it?

views.py:

from django.shortcuts import render
from django.http import HttpResponse

def test(request):
    return render(request, 'test.html')

test.html:

<html>
<head>
<script src="jquery.min.js"></script>
<script>

$(document).ready(function(){
    $("#gray").click(function(){
        window.open("http://www.w3schools.com");
    });
});

</script>
</head>

<body>
<div id='gray' style="width:100px;height:100px;background:gray;"></div>
</body>

urls.py:

from django.conf.urls import include, url
from secondv.views import test

urlpatterns = [
    url(r'^test', test),
]

The jquery.min.js file is in the template directory, the same directory as test.html file.

9
  • Side note : you haven't used ending script tag </script>. Commented Mar 5, 2016 at 9:01
  • modern browsers prevent javascript from opening new windows. Commented Mar 5, 2016 at 9:06
  • Is so, how can I open a website within a new tab by clicking on an image? Commented Mar 5, 2016 at 9:08
  • 1
    Check if jquery is loaded using console. Apparently, the problem is the way you are loading static file, i.e. jquery, in template. Read this : docs.djangoproject.com/en/1.9/howto/static-files Commented Mar 5, 2016 at 9:22
  • 1
    Yes, i've done the steps explained in doc (the link i gave in the comment above). Do as doc says. Commented Mar 5, 2016 at 10:53

1 Answer 1

3

Template directory is only for templates. All static content such as js, images should be placed to static directory. It's well described in docs

https://docs.djangoproject.com/en/1.9/intro/tutorial06/ https://docs.djangoproject.com/en/1.9/howto/static-files/deployment/ - for production

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.