I have an iframe that loads what a user requests. However, I need to make sure that the iframe does not load itself and is instead redirected to a default site.
The process has to be dynamic and work on any page, as I have multiple pages doing this, and custom code for each site wouldn't be practical.
As long as you are on the same domain as your parent, you can use parent. Add to the documents having iframes:
$(function() {
// Check if you are in an iframe, otherwise parent == self,
// and the next check will always return true.
function inIframe () {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}
// Check if parent.url == self.url.
function sameAsParent() {
try {
return parent.window.document.location.toString() === window.document.location.toString());
} catch (e) {
return false;
}
}
if (inIframe() && sameAsParent()) {
// You are being loaded by yourself! Redirect!
window.location.href = 'http://some-redirect-url.com'
}
});
Related
I am detecting the end of a webrtc stream in JavaScript like this...
stream.getVideoTracks()[0].onended = () => {
alert('Feed Has Ended');
};
This is working correctly, but if the user refreshes or reloads the page then the alert is also shown.
I understand that this is technically correct, but how can I get it to not display the alert under those conditions?
Why don't you use a global boolean to check if video is playing or not? When you will reload or refresh the page, isVideoRunning will become false and alert won't show.
Like
this.isVideoRunning = false;
On addtrack,
this.rtcPeerCon_.ontrack = function (event) {
if (!this.rtcPeerCon_) {
return;
}
if( !this.remoteVideo_ ) {
return;
}
this.remoteVideo_.srcObject = event.streams[0];
this.isVideoRunning = true;
}
then in your onStream ended callback you can check
if (this.isVideoRunning) {
alert('whatever');
this.isVideoRunning = false;
}
(I wanted this to be comment but I am not allowed to comment yet)
I am using following code to trigger the are you sure leaving website alert but for some reason its not recognising my if else condition in it and only works if I only put return true in window.onbeforeunload = function() { return true } . Is there a way I can trigger this alert only when user is navigating away from my website cause at the moment without if else condition its asking if user tries to navigate in the same website as well?
window.onbeforeunload = function() {
var location = window.document.activeElement.href;
if (typeof location != 'undefined')
{
console.log(location);
} else { reutn true; }
};
You can set a flag and toggle that flagged based on host of links that are clicked
var host = location.hostname,
allowNavigate = false;
window.onbeforeunload = function() {
if (!allowNavigate) {
return 'Message string';// not what actually gets displayed in most browsers these days
}
//don't return anything
return;
};
window.onload = function() {
document.querySelectorAll('a').forEach(function(a) {
a.addEventListener('click', function(e) {
allowNavigate = this.hostname === host;
});
});
};
The hostname on this page for example is "stackoverflow.com"
DEMO
You can add the "window.onbeforeunload" dynamically for the links you want to see the prompt message
and remove the "window.onbeforeunload" for the links you don't want prompt
<a onClick="a(true)" href="https://www.w3schools.com">Click here to get promt before navigate</a>
<br>
<a onClick="a(false)" href="https://jsfiddle.net/">Click here to navigate without promt </a>
<script>
function a(showPrompt){
window.onbeforeunload = showPrompt ? function(e) {return '';}: null;
}
</script>
https://jsfiddle.net/vqsnmamy/1/
Because of a new implementation of the cookie law, I have to gather consent for some cookies. One of the methods, is to wait for the user to scroll the page once, which has been legally meant to be an implicit consent.
I tried with:
jQuery(window).one('scroll', function(){
$(this).data('scrolled', true);
});
if(jQuery(window).data('scrolled')) {
console.log( 'ok' );
//place cookies
} else {
console.log( 'no' );
}
inside a script ( http://www.primebox.co.uk/projects/jquery-cookiebar/ ) that is called via:
$(document).ready(function(){
$.cookieBar({
});
});
However, console.log always tells me "no", regardless of the scrolling of the page.
Any hints?
Thanks in advance
Your code works fine if I change this like this:
<script>
jQuery(window).one('scroll', function()
{
$(this).data('scrolled', true);
if(jQuery(window).data('scrolled'))
{
console.log('ok');
//place cookies
}
else
{
console.log('no');
}
});
</script>
However I could not make out what $.cookieBar is doing ?
Check scroll and do some thing you want
window.onscroll = function (e) {
// called when the window is scrolled.
}
This will call every time when user scroll if you want to call it
only once then do some trict
Make a hidden Field
Now set value of this hidden field to true when scroll method call
window.onscroll = function (e)
{
if (document.getElementById("ScrollOnceCheck").value == "false")
{
document.getElementById("ScrollOnceCheck").value = "true";
// update cookie
}
}
I am creating a safari extension. When the user right-clicks on a link in safari, it should bring up the context menu. When the user clicks on "Get URL", it should open the clicked on url in a new window. I can't figure out how to get the url! It always opens "not found" instead.
injected.js :
document.addEventListener('contextmenu', handleContextMenu, false);
function handleContextMenu(event)
{
var target = event.target;
while(target != null && target.nodeType == Node.ELEMENT_NODE && target.nodeName.toLowerCase() != "a")
{
target = target.parentNode;
}
if(target.href)
{
safari.self.tab.setContextMenuEventUserInfo(event, target.href);
}
else
{
var foo = "href not found";
safari.self.tab.setContextMenuEventUserInfo(event, foo);
}
}
Global.html:
<!DOCTYPE HTML>
<script>
var lastUrl;
safari.application.addEventListener("contextmenu",handleContextMenu,false);
safari.application.addEventListener('command', handleCommand, false);
function handleContextMenu(event)
{
var query = event.userInfo;
lastUrl = query;
event.contextMenu.appendContextMenuItem("getUrl", "Get URL");
}
function handleCommand(event)
{
if(event.command === 'getUrl')
{
if (lastUrl)
{
safari.application.openBrowserWindow().activeTab.url = lastUrl;
}
else
{
safari.application.openBrowserWindow().activeTab.url = "not found";
}
}
}
</script>
How do I get the url? It always opens "not found" instead.
Why not just have var last url = event.userInfo in the handleCommand function? The userInfo should be defined at that point, and it should be more predictable that trying to set the value on the contextmenu event.
I don't understand why your code is not working, but there are a couple of things you might want to change anyway.
First, in the injected content script, if there's no target.href, don't bother calling safari.self.tab.setContextMenuEventUserInfo.
Second, in the global script, change your handleContextMenu function as follows:
function handleContextMenu(event) {
if (event.userInfo) {
event.contextMenu.appendContextMenuItem("getUrl", "Get URL");
}
}
That way, if the user didn't right-click a link, the context menu item won't be inserted.
Third, as Matt said, you don't need the lastUrl global variable, unless it serves some other purpose. You can refer to event.userInfo directly in handleCommand. And you don't need to check whether it's empty, because the context menu will only be inserted by handleContextMenu if it's not.
function handleCommand(event) {
if (event.command === 'getUrl') {
safari.application.openBrowserWindow().activeTab.url = event.userInfo;
}
}
Hope this helps.
I have a JavaScript fired popup window, it's created when hitting the onclick event of a tag:
top.popUp('/bus/popup.asp', '', 'height=457px,width=525px,status=no,scrollbars=no,resizable=yes,location=no,menubar=no,toolbar=no');return false;">
function popUp(url, winName, params, win)
{
var winExists = false;
if (win.handler)
{
if (!(win.handler == "" || win.handler.closed || win.handler.name == undefined))
{
winExists = true;
}
}
if (!winExists)
{
win.handler = window.open(url, winName, params)
}
try
{
win.handler.focus();
}
catch (e)
{
}
return false;
}
I also found these two funky little functions in the main file, fired when the popup window gets unloaded:
// Hack for onUnload and onBeforeUnload
onBeforeUnloadHappened = false;
closeDirectly = false;
function onBeforeUnloadHandler()
{
if (!closeDirectly)
{
openerUnhilightSaveLink();
onBeforeUnloadHappened = true;
}
}
function onUnloadHandler()
{
if (!closeDirectly)
{
if (!onBeforeUnloadHappened)
{
openerUnhilightSaveLink();
}
else
{
onBeforeUnloadHappened = false;
}
}
}
The problem is that in Safari, the window will only popup once, each subsequent click on the link gets you nada, zilch, nothing... how do I correct this?
I did find a couple of articles, Google is my friend, online and they suggested that I add this.close(), which I did to the unload event but it doesn't seem to have solved the problem.
The other thing I find odd is that the onUnloadHandler() doesn't actually happen each time the page is unloaded! Shouldn't this be called each time by the page itself irrespective of what else has happened?!
Thanks
It's my opinion that your popup code isn't written properly. This works in Safari, as I just tested it out. This is what I've done.
// Create a global popup object as you should rarely ever
// have more than one popup open at a time.
window.popUpObj = null;
// The function to call the popup.
function popUp(url, winName, params)
{
// If the window has not been defined or if it has been closes open it.
// Since we defined window.popUpObj to be null we'll expect only 2 conditions.
if(window.popUpObj == null || window.popUpObj.closed)
{
window.popUpObj = window.open(url, winName, params);
}
// Get window focus
window.popUpObj.focus();
// Return false.
// we do this so that when we call it we don't need a ; to return false afterwards
// we just add return before the function call. It's a bit more elegant.
return false;
}
You can call it like this. You should by the way always populate the 2nd parameter with something.
Link
Here's a working HTML doc.
<html>
<head>
<script type="text/javascript">
window.popUpObj = null;
function popUp(url, winName, params)
{
// If the window has not been defined or if it has been closes open it.
if(window.popUpObj == null || window.popUpObj.closed || window.popUpObj.name == undefined)
{
window.popUpObj = window.open(url, winName, params);
}
// Get window focus
window.popUpObj.focus();
// Return false.
return false;
}
</script>
</head>
<body>
Link
close
</body>
</html>