Jump to content

Send parameter to script


Recommended Posts



I'm working in a website development with django and I need to send a parameter to a script. On my web, I send parameters through the URL on this way:


I put the href in which {{obra.id}} is the object received from views.py




At the url I take that object as parameter to go to one page or another, according its value,


url(r'^edicionobra/obra(?P<id_obra>.*)/$','demo.apps.obras.views.EdicionObra', name='edicionobra'),



However, I would like to take that parameter to use it in a script, but I don't know how to do it.


Would anybody know how to solve this?


Thank you very much!



Link to comment
Share on other sites

You mean using query parameters in Javascript? You can get anything from the page's address by using the location object. Like the search property, in your case


Link to comment
Share on other sites

Hello, first of all, thank you for your answer. I've been looking for examples and I've found a code.

I've tried to change it to adapt it to my URL but I always receive the same: null


Could you help me with my URL? http://localhost:8000/edicioncuenca/obra=2/cuenca=1/

function getParameter(paramName) {  var searchString = window.location.search.substring(1),    i, val, params = searchString.split("/");  for (i=0;i<params.length;i++) {    val = params[i].split("=");    if (val[0] == paramName) {      return val[1];    }  }return null;var cuenca=getParameter("cuenca");alert(cuenca);

Thank you again!

Link to comment
Share on other sites

Add some debugging, add some alert or console.log statements (or use breakpoints in your browser's developer tools) to figure out what the values of all of those variables are. Check what searchstring, params, val, etc are. In your case, for that URL, window.location.search is empty. Look at window.location to see what other options you have instead of search.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...