I wasn't suggesting he should use the URL() function, just pointing out 
that it *can't* be used to build a URL on the client side, and showing a 
simple example of how to build a URL on the client side via Javascript (use 
of the URL() function was incidental and not central to the point of the 
example). In any given case, it may or may not make sense to use the URL() 
function to build the base URL (vs. hard-coding it in a .js file), and I 
wouldn't state a blanket rule either way (if you're using the web2py 
rewrite system, it's generally a good idea to use URL() whenever feasible).

Also, I hadn't realized you made an update to my code example. The update 
wouldn't work because it attempts to put Javascript variables inside Python 
code that must be executed on the server.

Anthony

On Sunday, May 20, 2012 8:56:51 PM UTC-4, Bruce Wade wrote:
>
> Well honestly if it was a dynamic link I wouldn't even use URL as good 
> style of JS programming is putting the code in a .js if this is the case 
> then the web2py parser cannot even access the URL to parse it. (At least I 
> have never been successful with that process.)
>
> If they are getting the params at a later time and in a js file there is 
> no need in using the URL just use url = '/path/' + param + '/' + param; The 
> only problem with this is if he changes the path, however if he changes the 
> path he would be required to update URL() anyways.
>
> I have fell into the trap on trying to rely on the helpers to much myself 
> in the past. Some places in your code you shouldn't use them. Trying to use 
> them for everything kind of forces you to change your logic to bend around 
> web2py instead of having web2py work with your logic. Also whenever you use 
> URL instead of the direct url='/path' the framework is forced to do more 
> function calls, in some situations like this one it isn't really worth 
> having the extra function call and it is actually more typing :D
>
> On Sun, May 20, 2012 at 5:19 PM, Anthony <abasta...@gmail.com> wrote:
>
>> He said param1 and param2 are determined on the client side -- which 
>> means they cannot be set on the server side by web2py. Of course, I don't 
>> know what he's actually doing on the client side, so this exact code may or 
>> may not be relevant. Do you have an alternative suggestion?
>>
>> Anthony
>>
>> On Sunday, May 20, 2012 7:57:10 PM UTC-4, Bruce Wade wrote:
>>>
>>> Why would you do the code like that?? 
>>>
>>> Updated: 
>>> <script>
>>> jQuery(function() {
>>>   var param1 = 'hello'
>>>   var param2 = 'world'
>>>   jQuery('a#mylink').attr('href'**, '{{=URL("action", args=["' + param1 
>>> + '","' + param2 + '"])}}');
>>> });
>>> </script>
>>>
>>> <h3>{{=A('...', _href='', _id='mylink')}}</h3>
>>>
>>>
>>> On Sun, May 20, 2012 at 4:36 PM, Anthony <abasta...@gmail.com> wrote:
>>>
>>>>
>>>>
>>>> On Sunday, May 20, 2012 3:43:38 PM UTC-4, Ashraf Mansour wrote:
>>>>>
>>>>> thanks for the immediate reply.
>>>>>
>>>>> param1 and param2 are determined on the client side.
>>>>>
>>>>> how can it be generated by javascript?
>>>>>
>>>>>
>>>> It depends. Here's one example:
>>>>
>>>> <script>
>>>> jQuery(function() {
>>>>   var param1 = 'hello'
>>>>   var param2 = 'world'
>>>>   jQuery('a#mylink').attr('href'**, '{{=URL("action")}}' + '/' +param1 
>>>> + '/' + param2);
>>>> });
>>>> </script>
>>>>
>>>> <h3>{{=A('...', _href='', _id='mylink')}}</h3>
>>>>
>>>> When the page is loaded, the href of the mylink anchor tag will be 
>>>> replaced with the "action" URL along with the values of param1 and param2 
>>>> appended. Of course, you'll need some way to set the values of param1 and 
>>>> param2, and there may be some other event that should trigger the 
>>>> replacement -- it depends what you're trying to do.
>>>>
>>>> Anthony
>>>>
>>>>
>>>
>>>
>>> -- 
>>> -- 
>>> Regards,
>>> Bruce Wade
>>> http://ca.linkedin.com/in/**brucelwade<http://ca.linkedin.com/in/brucelwade>
>>> http://www.wadecybertech.com
>>> http://www.fittraineronline.**com <http://www.fittraineronline.com> - 
>>> Fitness Personal Trainers Online
>>> http://www.warplydesigned.com
>>>
>>>
>
>
> -- 
> -- 
> Regards,
> Bruce Wade
> http://ca.linkedin.com/in/brucelwade
> http://www.wadecybertech.com
> http://www.fittraineronline.com - Fitness Personal Trainers Online
> http://www.warplydesigned.com
>
>

Reply via email to