Open href link in popup - javascript

I'm trying to open the link in popup, i've write this static html code :
<a href="http://www.google.com" target="popup"
onclick="window.open('http://www.google.com','popup','width=600,height=600');
return false;"> Open Link in Popup
</a>
this code works, it open the link in popup.
But, if i want to add this code in javascript (with jquery) like this :
$('#services').append("<li><a href='http://www.google.com' target='popup' onclick='window.open('http://www.google.com','popup','width=600,height=600'); return false;'>Open Link in Popup</a></li>");
The link is opened in new tab and not in popup.
Why ?

Check this example on jsfiddle - Problem is in quotes
$('#services').append("<a href='http://www.google.com' target='popup' onclick=window.open('http://www.google.com','popup','width=600,height=600'); return false;>Open Link in Popup</a>");

You missed to escape the quotes in onClick of your jQuery. Update the script as below.
$('#services').append("<li>Open Link in Popup</li>");

Change your algorithm, instead of putting the javascript inside onclick, add a class or ID, with data-, so you can attach your jQuery event handler on that class/ID, and use the data to create new window. Imagine the data as data-destination="http://www.destination".
That should fix your problem.

Related

Javascript change link destination

I am trying to make a javascript that goes makes <a> go to another link, rather than the one specified in its href. E.g. I want
<a href="http://www.example.com/some-page">
To go to instead:
http://www.newexamplesite.com/http:%252F%252Fwww.example.com%252Fsome-page
Unfortunately I don't have access to jQuery for this application. Has to be pure javascript.
I'm looking for some javascript that can be included within the actual <a> tag:
<a onclick="" href="http://www.example.com/some-page">
Note that the original link has to be detected and all / is needed
to be replaced with %252F
Try this:
<a onclick="window.location.href = 'http://newlink.com/' + encodeURIComponent(this.href); return false;" href="http://www.example.com/some-page/">Go to new link</a>
Try the following:
<a onclick="event.preventDefault();location.href='http://www.uol.com.br/'" href="http://www.example.com/some-page">Link Text</a>
The event.preventDefault should stop the anchor element's default behaivour (ie - directing the user to the site within the a href.
EDIT: forgot parentheses on function call!
You should prevent a default event (i.e. going to the link specified by href) and go to another link.
<a onclick="event.preventDefault();location.href='http://www.newexamplesite.com/'+this.href.replace(/\//g,'%252F')"
href="http://www.example.com/some-page">
test
</a>
You can replace the href URL with a function call like this:
<a href="javascript:goToLink()" />
<script>
function goToLink(){
// your link selection logic goes here
.....
// when you have selected the link you want to go to assign it to the window location.href attribute and the browser will redirect the user to the specified link
window.location.href = "..selected link...";
}
</script>

Javascript & CGI How to open div and pass arguments in same time

I have to change my code a little bit to add jquery popup window.
So far I just open new window using:
<a href="?action=_edit&_code=code"><img src...
In new version I simply open popup window using:
<a href="#myDialog" data-toggle="modal" onclick="">...
It works in terms of open dialog box, the only problem remains is how to pass arguments from ?action=_edit&_code=code in this case ?
Thanks for help ;)
You can use more data tags, for example, data-action, and data-code. These can then be obtained in your on click handler.
This link here might give you a better idea: http://www.slideshare.net/lensco/html5-data-attributes

Use href NOT onClick in hyperlink - Javascript

I have the following HTML that pops up a window when I click the 'Open Sesame' link:
Open Sesame</li>
However, I was wondering is it possible I could remove the 'onClick' part and just have the javascript within the href & still get the same new window effect. Is this possible?
Many thanks for any pointers
Yes you can:
Open Sesame
http://jsfiddle.net/Curt/NqfMX/
However, I would recommend an event listener for this. It makes your HTML code much tidier and seperates the function from the HTML design:
<a class="opensesame" href="/about-us/" title="Find out About Us">Open Sesame</a>​​​​​​​​​​​​​​​​​​​​​​
​$(function(){
$("a.opensesame").click(function(e){
e.preventDefault();
window.open('/about-us/','1329304840803','width=1050,height=500,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=0,top=0');
});
});​
http://jsfiddle.net/Curt/NqfMX/1/

how to prevent popup javascript onclick event

i have link
<a onclick="return package_tour_order(14400287396);" href="javascript:;">advanced</a>
<a>doit</a>
need to get value from onclick only ID nubmer, but this
return package_tour_order(14400287396) is javascript function that popup new window with informationabout that ID
if i run this code
$('a:contains("advanced")').attr('onclick').match(/\d+(?=\))/)[0]
it works fine but when i run in my project with that javascript function(this function is loaded from another site) its does not work and popup window
i need to disable onclick somehow and get only that ID number, how can i doo that?
Don't make it a link. Make it a span or whatever. Also... what do you mean by "function is loaded from another site". If the script is loaded that should not influence things.

What is target="_new"? validator giving error

What is target="_new"? Validator is raising an error..
How do you do this with jquery because Validator is raising an error.
On the same page, I have target="_new" and target="_blank". target="_new" is in that form code which i received from email newsletter company.
I'm using this for target="_blank"
$(function() {
$('a[href^=http]').click( function() {
window.open(this.href);
return false;
});
});
What should i do for target="_new"
Update: 1 min Ago
upon clicking on submit button i want to open a page in new window to pass validation how to do this in jquery as i'm doing for other external link.
update:
this is code
<form method="post" class="form-wrapper" action="http://sitename.com/form.php">
There is no such thing as target="_new". Using this will simply open the link in a new window called "_new". You might notice that clicking on a link with target="_new" will open a new window (or tab) but then a second link will open in the same window (or tab), rather than opening a second one as you'd probably expect.
Personally, I don't think you should be specifying target="_blank" at all - let the user choose.
target="_blank" is invalid in XHTML (actually, the entire target attribute is invalid). If you want a link to open in a new window (usually a bad idea, in my opinion) and validation is important to you, the only way to do it is with javascript (like you did).
This is an attribute for a(anchor) tag, which let you open the page on the same window or in a new window.

Categories