R
R
Roman Koff2015-09-04 15:36:47
JavaScript
Roman Koff, 2015-09-04 15:36:47

How to implement a replacement in the code of the page according to the dictionary in JavaScript?

Tell me how to correctly implement the replacement of a string in the html page code (after loading into the browser) with an expression from a dictionary. And how to properly organize the storage of this dictionary (in the future, I plan to load the dictionary from a JSON request).

For example, the dictionary contains the entry:

var dict = {
  'Невский 123': '<a href="http://maps.yandex.ru/...">Невский пр., д. 123</a>',
  'Санкт-Петербург': '<a href="www.visit-petersburg.ru/ru">Санкт-Петербург</a>',
};


The page contains the following code: After loading the page in the browser and executing the script, the code will be replaced with the following:
<p>Адрес: Невский 123</p>

<p>Адрес: <a href="http://maps.yandex.ru/...">Невский пр., д. 123</a></p>


I use jQuery in my project.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Deodatuss, 2015-09-04
@Zarinov

bullshit of course..

G
GreatRash, 2015-09-04
@GreatRash

I think that it is necessary to store as a key not the phrase that needs to be replaced, but the id of the element.

R
Roman Koff, 2015-09-04
@Zarinov

I don't really understand what the id has to do with it? There are some expressions on the page that need to be replaced with some other expressions. For example, if it says St. Petersburg, it should be replaced with the link <a href=' www.visit-petersburg.ru/ru '>St. Petersburg</a>, because the key 'St. Petersburg' is in the dictionary.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question