Похожие чаты

If I pass data using data-* attribute in script tag

then can I access this data in my package which is react app? If it is simple javascript file then I can access but in react app is it possible?

13 ответов

20 просмотров

It's still just JavaScript, anything's possible; but you may be doing something weird. This is an XY question. Tell us what you want to do instead of how.

Adi- Автор вопроса
MUTHU・KUMAR 「メーカー」 🔺
It's still just JavaScript, anything's possible; b...

I am creating react widget in which I am loading my widget on website using CDN script tag. Now this widget is react app. I want to pass token using data-* attribute to react app how can I do that ? script tag code: <script src="https://unpkg.com/mypackage@1.0.1/index.js" data-token="100200"></script> now how to access this token attribute in react app? I am using webpack with react app so I am generating single bundle.js file. Let me know if you need some more info I think this is sufficient

Adi
I am creating react widget in which I am loading m...

If it'll be a single bundle you can do document.currentScript.dataset.token

Adi- Автор вопроса
MUTHU・KUMAR 「メーカー」 🔺
If it'll be a single bundle you can do document.c...

But in which file of my react app? Can I access it in app.js file?? Which file can access this token!?

Adi
But in which file of my react app? Can I access it...

Anywhere should work, saving it in a variable at top level would be best

Adi- Автор вопроса
MUTHU・KUMAR 「メーカー」 🔺
Anywhere should work, saving it in a variable at t...

Anywhere should work?? But could not understand how anywhere should work?? I was thinking it will be available only in app.js

Adi- Автор вопроса
MUTHU・KUMAR 「メーカー」 🔺
It is bundled into a single script, yes?

Yes i am using webpack and it will generate single bundle.js file in dist folder

Adi- Автор вопроса
Adi
Oh ok got it. Let me try it out. Thanks for helpin...

Just be sure to not use it from an event handler; since the element that triggers the event will be the target then. So in case you need to use it inside an event handler, assign it to a variable at the top level and pass it down

Adi- Автор вопроса
MUTHU・KUMAR 「メーカー」 🔺
Just be sure to not use it from an event handler; ...

No in my case I am passing token as data attribute into my react app using CDN script tag and then once token is available in app.js I will display different components based on different tokens in react widget thats the plan. Anything you would like to suggest or looks good?

Похожие вопросы

Обсуждают сегодня

а через ESC-код ?
Alexey Kulakov
29
30500 за редактор? )
Владимир
47
Чёт не понял, я ж правильной функцией воспользовался чтобы вывести отладочную информацию? но что-то она не ловится
notme
18
У меня есть функция где происходит это: write_bit(buffer, 1); write_bit(buffer, 0); write_bit(buffer, 1); write_bit(buffer, 1); write_bit(buffer, 1); w...
~
13
any reference of this implementation?
BitBuddha
29
Ⓐrtto, [4/23/24 7:02 PM] Please explain more fully how it is not working exactly, and what are the steps you are taking, and what error messages come or what happens. Ⓐrtto, ...
Ezza Kezza
2
sounds like people have lost their kaspa on tradeogre... does this mean tradeogre not trustworthy?
Ezza Kezza
15
Страшнейшая правда про списки ЦБ. С первых дней жизни P2P сферы, молодые человеки, начитавшись законодательной базы и "внутренних" документов, решили, что им противостоит сер...
Foxcool
3
Недавно Google Project Zero нашёл багу в SQLite с помощью LLM, о чём достаточно было шумно в определённых интернетах, которые сопровождались рассказами, что скоро всех "ибешни...
Alex Sherbakov
5
So much speculation in the last week. So much volatility in price. This is because Hedera has a GC that isn't using the network it's governing. Why aren't people asking why a...
Summit Seeker R
9
Карта сайта