Jump to content

Recommended Posts

Hello,

 

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

 

href="/edicionobra/obra{{obra.id}}/">

 

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 post
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

http://www.w3schools.com/jsref/obj_location.asp

Link to post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...