0

1)i have printed links after processing on the screen as follows-

for i in index:
   self.response.out.write('<a id="w3s" href="' + i + '">' +i+' class="testClick" </a><br>')

where index is list of processed urls. The url where results are displayed is like mysite.com/sign

2)i want to get which link user clicked. This is what i tried to do-

<script>
var addressValue;
$(document).ready(function(){
  $("a").click(function(){
    addressValue = $(this).attr("href");
  });    
});
</script>

I have got the clicked url in addressValue variable but what should i do next. I could not get how to use GET or POST methods to get the variable server side; and then how to get the string in my python variable.

Using GET/POST:(Not Working)

<script>

var addressValue;

$(document).ready(function(){
  $("a").click(function(){
    addressValue = $(this).attr("href");

//Method-I
    $.post("https://mysite.com/sign",
    {
        fooParameter: addressValue,
    },
    function(data,status){
        alert("Data: " + data + "\nStatus: " + status);
    });
  });    
});


//Method-II
$.ajax({
    url: "https://mysite.com/sign",
    method: "POST",
    data: {fooParameter: addressValue},
});

</script>

And then accessing variable in python string through following command

selectedlink=self.request.get('fooParameter')

Where am i mistaking or is there any other way? Note: I am using Google App engine

3
  • I am not sure I understnad you but what you are looking for is pure Javascript. If you use the jQuery library, you can capture the clicked urls and post it to the server in JSON format. api.jquery.com/click api.jquery.com/jQuery.post Commented Mar 18, 2014 at 17:07
  • api.jquery.com/click: this i have implemented successfully but api.jquery.com/jQuery.post -> for GET/POST, i have problem implementing this. i have pasted the code above. Please check if u could help Commented Mar 18, 2014 at 17:47
  • Could it be a CORS issue (en.wikipedia.org/wiki/Cross-origin_resource_sharing)? You would have to allow Cross-Origin Requests by returning a Access-Control-Allow-Origin header from python backend. Commented Mar 18, 2014 at 18:05

1 Answer 1

1

There is another solution, which does not need javascript. Create a link to your own handler, which redirects to the query string.

Example:

<a target="_blank" href="/redir_url?http://www.example.com/address.html">Example address</a>

The Handler:

class RedirUrl(webapp2.RequestHandler):

    def get(self):

        url = self.request.query_string
        logging.info('user clicked ' + url)
        self.redirect(url)        
Sign up to request clarification or add additional context in comments.

2 Comments

thank you very much! its working. is there any other way also. redirection is quite slow and visible.
Redirection Visible? Slow?

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.