Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #39763

Re: Create cookie from ID of <a> tag

From The Natural Philosopher <tnp@invalid.invalid>
Newsgroups comp.lang.javascript
Subject Re: Create cookie from ID of <a> tag
Date 2021-03-25 09:13 +0000
Organization A little, after lunch
Message-ID <s3hk8a$g7n$1@dont-email.me> (permalink)
References <06c15260-54cb-43fd-99f0-cba71b0caa36n@googlegroups.com> <s3fudf$eg3$1@dont-email.me> <01863a61-d1bf-47ec-9955-1a1579e9aca2n@googlegroups.com>

Show all headers | View raw


On 25/03/2021 08:05, Daniele Ferrarese wrote:
> Il giorno mercoledì 24 marzo 2021 alle 18:55:02 UTC+1 The Natural Philosopher ha scritto:
>> On 24/03/2021 16:36, Daniele Ferrarese wrote:
>>> Hello, I'm a beginner with js. I was wondering if it was possible to create a cookie by taking the value from the ID of the <a> tag.
>>> Thanks for your help.
>>>
>>> Rgds Daniele
>>>
>> Normally cookies are set by the server and stored on the client, but
>> javascript can set a cookie independently of that:
>>
>> https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie
>>
>> I am not clear on what the 'id of the A tag' actually means.
>>
>> Do you mean there is something like <a href=" blah.com"
>> id="uttercrap">cobblers</a> and you want to set a cookie to 'uttercrap'
>>
>> The problem is how will you know which <a></a> element to refer to,
>> unless you *already* know its id?
>>
>>
>> -- 
>> A lie can travel halfway around the world while the truth is putting on
>> its shoes.
> ----------------------------------------------
> I thank you for your kind reply. You understood perfectly. The <a> element is the calling element therefore is calling itself.
> How it can call itself and create a cookie with "value" its ID? Something like this ?
> <a href="javascript: var x = this.id; document.cookie="language="+x"";></a>
> Of course I tried this solution but is not running.
> Form will submit this value to server and response will update all texts in corresponsing language.
> 
> Can you tell me where is the mistake.

OK

Why are you using an <A> element just to set a cookie?
Do you actually want to reload the web page after setting a cookie?

Anyway no matter whether is an <a> element or any other element that you 
want the user to click on, what you need is an onclick=" statement

so

onclick="document.cookie='language='+this.id".

If you want to call the same web page again with this cookie set, then

<a href="#" onclick="document.cookie='language='+this.id; 
window.location.reload(true)" > My Language</a>

should work with all browsers. And it doesn't have to be in an 
<a>element</a>, since the reload is being done by javascript

So
<div onclick="document.cookie='language='+this.id; 
window.location.reload(true)"> click here to set cookie and reload 
page</div>

should work just as well


> 


-- 
"Anyone who believes that the laws of physics are mere social 
conventions is invited to try transgressing those conventions from the 
windows of my apartment. (I live on the twenty-first floor.) "

Alan Sokal

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Create cookie from ID of <a> tag Daniele Ferrarese <daniele.seversrl@gmail.com> - 2021-03-24 09:36 -0700
  Re: Create cookie from ID of <a> tag The Natural Philosopher <tnp@invalid.invalid> - 2021-03-24 17:54 +0000
    Re: Create cookie from ID of <a> tag Daniele Ferrarese <daniele.seversrl@gmail.com> - 2021-03-25 01:05 -0700
      Re: Create cookie from ID of <a> tag The Natural Philosopher <tnp@invalid.invalid> - 2021-03-25 09:13 +0000
        Re: Create cookie from ID of <a> tag Daniele Ferrarese <daniele.seversrl@gmail.com> - 2021-03-25 02:21 -0700
          Re: Create cookie from ID of <a> tag Daniele Ferrarese <daniele.seversrl@gmail.com> - 2021-03-25 03:23 -0700
            Re: Create cookie from ID of <a> tag Daniele <webmaster@appliserver.com> - 2021-03-26 09:19 +0100
              Re: Create cookie from ID of <a> tag Daniele Ferrarese <daniele.seversrl@gmail.com> - 2021-03-26 01:57 -0700
              Re: Create cookie from ID of <a> tag Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-03-26 17:33 +0100
                Re: Create cookie from ID of <a> tag Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-03-26 17:20 +0000
                Re: Create cookie from ID of <a> tag Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-03-26 19:35 +0100
                Re: Create cookie from ID of <a> tag Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-03-26 23:22 +0100
                Re: Create cookie from ID of <a> tag "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-03-26 15:39 -0700
                Re: Create cookie from ID of <a> tag Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-03-31 00:58 +0200
                Re: Create cookie from ID of <a> tag "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-03-30 19:58 -0700
                Re: Create cookie from ID of <a> tag Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-04-01 10:09 +0200
                Re: Create cookie from ID of <a> tag "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-04-01 06:11 -0700
                Re: Create cookie from ID of <a> tag Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-04-01 19:34 +0200
              Re: Create cookie from ID of <a> tag The Natural Philosopher <tnp@invalid.invalid> - 2021-03-26 17:10 +0000
                Re: Create cookie from ID of <a> tag Daniele <webmaster@appliserver.com> - 2021-03-27 12:16 +0100
                Re: Create cookie from ID of <a> tag The Natural Philosopher <tnp@invalid.invalid> - 2021-03-27 13:25 +0000
                Re: Create cookie from ID of <a> tag "Mark-" <nospam@mapson.xyz> - 2021-03-31 13:26 +0000
              Re: Create cookie from ID of <a> tag Daniele <webmaster@appliserver.com> - 2021-03-27 12:08 +0100
  Re: Create cookie from ID of <a> tag Man From a city with name Viljandi <ya12983@mail.com> - 2021-03-28 06:26 -0700

csiph-web