Can I do this without jQuery? - javascript

I use the following code on my site. I'm wondering if I need jQuery to do it or if standard javascript can handle the process.
<script type='text/javascript'>
//<![CDATA[
$(window).load(function(){
$("a[href^='http']").click(function(event) {
event.preventDefault(); // prevent the link from opening directly
// open a pop for the link's url
var popup = window.open( this.href , "", "toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,status=no,width=340,height=10,left=250,top=175" );
// popup.blur();
// window.focus();
}); }); //]]>
</script>
It's from this page: Pop Under on Click for RSS Feed - Javascript

Yes, and it’s relatively simple: just use document.getElementsByTagName('a') and traverse the array you get, seting onclick for any elements there that have an href attribute with a value that starts with http. And make this a function that is called via the onload attribute in <html> for example.

var hrefs = document.getElementsByTagName('a');
for (i in hrefs) {
if (hrefs[i].href && hrefs[i].href.match(/^http/)) {
hrefs[i].onclick = function(){
var popup = window.open( this.href , "", "toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,status=no,width=340,height=10,left=250,top=175" );
return false;
}
}
}

you can try this
<div id="divid" onclick="showpop();">click me</div>
<script type="text/javascript">
function showpop(){
window.open(arguments);
return false;
}
</script>
document.getElementById(eleID).onClick = function (){
//do stuff
}

Related

Strange bug with jquery accessing window

I have this code http://jsfiddle.net/xxL6e2fk/
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
janela = window.open("https://www.sitepor500.com.br");
window.setTimeout(
function() {
alert($(janela.window.document).text());
alert($(janela.window.document).html());
},
5000
);
</script>
It just opens a window and tries to get the "text" and "html" of its content. The "text" is displaying the content correctly but the "html" is not. Anyone has any idea how to solve this problem?
there is no html on janela.window.document try using janela.window.document.documentElement instead or targeting the body tag within the new window.
$("body", janela.window.document.documentElement).html()
You can use something like this:
var janela = window.open('https://www.sitepor500.com.br');
janela.onload = function () {
setTimeout(function () {
console.log(janela.document.documentElement.outerHTML)
}, 2000);
}
But this can work only if you are asking it from the same domain (sitepor500.com.br)

How can I make jQuery open only post links in new tabs in Wordpress for this code

I have this code in my header.php :
<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("a").click(function(){
jQuery("a").attr("target","_blank");
url=jQuery(this).attr('href');
jQuery(this).attr('href','/te3/out.php?l=click&u=' + escape(url));
});
});
</script>
And right now it opens all links in new browser tabs. What I would like is to open only the posts. The permalink looks like mywebsite.com/this-is-the post/ if this helps in any way.
Please help
Thanks
You can filter links with jQuery's .filter() method. Just customize the regular expression to fit your actual needs
jQuery(document).ready(function() {
jQuery("a")
.filter(function() {
var href = jQuery(this).attr('href');
// return true if href exists and matches the regular expression
return href && href.match(/mywebsite\.com\/[\w-]+/);
})
.click(function(){
jQuery("a").attr("target","_blank");
url = jQuery(this).attr('href');
jQuery(this).attr('href','/te3/out.php?l=click&u=' + escape(url));
});
});
Update
If you want the opposite behavior, you can use .not()
jQuery(document).ready(function() {
jQuery("a")
.not(function() {
var href = jQuery(this).attr('href') || '';
// return true if href exists and matches the regular expression
return href.match(/mywebsite\.com\/[\w-]+/);
})
.click(function(){
jQuery("a").attr("target","_blank");
url = jQuery(this).attr('href');
jQuery(this).attr('href','/te3/out.php?l=click&u=' + escape(url));
});
});

How to call window load event on specific page

I want to call windows.load event on specific page. I am doing like this ..
$(window).load(function(){
if(document.URL == "/car-driving.html")
{
overlay.show();
overlay.appendTo(document.body);
$('.popup').show();
return false;
}
});
Here I am trying to show overlay based on page load, but its not working. If I remove if condition then overlay is showing in every page. How to make it specific to only my car-driving.html page so that if anyone visit car-driving.html page, then it will show overlay ?
Try
if (window.location.href.match('car-driving.html') != null) {
overlay.show();
overlay.appendTo(document.body);
$('.popup').show();
return false;
}
Hope It Helps
document.URL returns the whole url ex: "h t t p : / / stackoverflow.com/questions/17970734/how-to-call-window-load-event-on-specific-page"
You'll have to use a regex match or indexOf
edit or better yet
document.location.pathname ==
Try displaying the value of the href in an alert and you will see what you need to match:
$(document).ready(function(){
alert(window.location.href);
});
Hello i Use this and working well for me
<script>
$(window).load(function() {
setTimeout(function() {
$('#onload').modal('show');
}, 10000);
}
function delayer() {
window.location = 'Your Desired URL';
}
);
</script>
Here .model is my popup div class

Creating a same-page event with JQuery

I am trying to make jquery trigger an event when an anchor link has the same URL as the current page. Eventually this will add a class to the anchor link and let me style it differently with CSS but for now I just want it to show an alert box. Code below doesn't seem to work:
<script type="text/javascript">
$(document).ready(function() {
var buildurl = window.location.href;
if($('a').attr('href') = buildurl){
alert("Done");
}
});
</script>
Can anybody shine a light on why this isn't working? Or a best practice for similar using JQuery?
Link is: http://www.otahboy.com/shop/index.php?route=information/information&information_id=4
Cheers,
Jack
<script type="text/javascript">
$(document).ready(function() {
var buildurl = window.location.href;
if($('a').attr('href') == buildurl){
alert("Done");
}
});
</script>
use equality operator ==
if($('a').attr('href') == buildurl){
alert("Done");
$('a').attr("color","Red");
}
You have an assignment in your if condition. You need to add one = to it.
<script type="text/javascript">
$(document).ready(function() {
var buildurl = window.location.href;
if($('a').attr('href') == buildurl){
alert("Done");
}
});
</script>

Open popup and refresh parent page on close popup

I opened a popup window by window.open in JavaScript, I want to refresh parent page when I close this popup window.(onclose event?) how can I do that?
window.open("foo.html","windowName", "width=200,height=200,scrollbars=no");
You can access parent window using 'window.opener', so, write something like the following in the child window:
<script>
window.onunload = refreshParent;
function refreshParent() {
window.opener.location.reload();
}
</script>
The pop-up window does not have any close event that you can listen to.
On the other hand, there is a closed property that is set to true when the window gets closed.
You can set a timer to check that closed property and do it like this:
var win = window.open('foo.html', 'windowName',"width=200,height=200,scrollbars=no");
var timer = setInterval(function() {
if(win.closed) {
clearInterval(timer);
alert('closed');
}
}, 1000);
See this working Fiddle example!
on your child page, put these:
<script type="text/javascript">
function refreshAndClose() {
window.opener.location.reload(true);
window.close();
}
</script>
and
<body onbeforeunload="refreshAndClose();">
but as a good UI design, you should use a Close button because it's more user friendly. see code below.
<script type="text/javascript">
$(document).ready(function () {
$('#btn').click(function () {
window.opener.location.reload(true);
window.close();
});
});
</script>
<input type='button' id='btn' value='Close' />
I use this:
<script language='javascript'>
var t;
function doLoad() {
t = setTimeout("window.close()",1000);
}
</script>
<script type="text/javascript">
function refreshAndClose() {
window.opener.location.reload(true);
window.close();
}
</script>
<body onbeforeunload="refreshAndClose();" onLoad='doLoad()''>
when the window closes it then refreshes the parent window.
window.open will return a reference to the newly created window, provided the URL opened complies with Same Origin Policy.
This should work:
function windowClose() {
window.location.reload();
}
var foo = window.open("foo.html","windowName", "width=200,height=200,scrollbars=no");
foo.onbeforeunload= windowClose;​
In my case I opened a pop up window on click on linkbutton in parent page.
To refresh parent on closing child using
window.opener.location.reload();
in child window caused re open the child window (Might be because of View State I guess. Correct me If I m wrong).
So I decided not to reload page in parent and load the the page again assigning same url to it.
To avoid popup opening again after closing pop up window this might help,
window.onunload = function(){
window.opener.location = window.opener.location;};
If your app runs on an HTML5 enabled browser. You can use postMessage. The example given there is quite similar to yours.
Try this
self.opener.location.reload();
Open the parent of a current window and reload the location.
You can reach main page with parent command (parent is the window) after the step you can make everything...
function funcx() {
var result = confirm('bla bla bla.!');
if(result)
//parent.location.assign("http://localhost:58250/Ekocc/" + document.getElementById('hdnLink').value + "");
parent.location.assign("http://blabla.com/" + document.getElementById('hdnLink').value + "");
}
You can use the below code in the parent page.
<script>
window.onunload = refreshParent;
function refreshParent() {
window.opener.location.reload();
}
</script>
Following code will manage to refresh parent window post close :
function ManageQB_PopUp() {
$(document).ready(function () {
window.close();
});
window.onunload = function () {
var win = window.opener;
if (!win.closed) {
window.opener.location.reload();
}
};
}

Categories

Resources