2
<script src="https://code.jquery.com/jquery-3.0.0.js"></script>
<script type="text/javascript">
var link = $('#unique_link').html();
var vk_link = "http://vk.com/share.php?url="+link+"&amp;title=text";
</script>
<a onclick="window.open(vk_link,'_blank', 'scrollbars=0, resizable=1, menubar=0, left=100, top=100, width=550, height=440, toolbar=0, status=0');return false">LINK</a>

But in browser I see undefined, not replaced variable: window.open(vk_link, .... How to fix it?

asked Apr 30, 2020 at 16:52
1
  • Also, make sure you're running this code after the DOM has actually rendered. Commented Apr 30, 2020 at 17:05

2 Answers 2

2

You're trying to access vk_link in a string which will not be evaluated to its value. Just define a function let's say openWindow and call it on onClick instead like below.

<script src="https://code.jquery.com/jquery-3.0.0.js"></script>
<script type="text/javascript">
var link = $('#unique_link').html();
var vk_link = "http://vk.com/share.php?url="+link+"&amp;title=text";
function openWindow(){
 window.open(vk_link,'_blank', 'scrollbars=0, resizable=1, menubar=0, left=100, top=100, width=550, height=440, toolbar=0, status=0');
}
</script>
<a onclick="openWindow()">LINK</a>

Hope this helps !

answered Apr 30, 2020 at 17:02
Sign up to request clarification or add additional context in comments.

Comments

2

You can also use the HREF attribute with javascript: keyword in Anchor Tag to call a JavaScript function:

<a href="javascript:window.open(vk_link,'_blank', 'scrollbars=0, .......">Link</a>
answered Apr 30, 2020 at 17:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.