I got an application that serves widgets inside iframes of other websites. So far so good but how can I allow these widgets views only to be loaded inside an iframe and not directly?
This should work
<iframe src="http://www.example.com/widgets/example">
But typing in http://www.example.com/widgets/example directly into a browser shouldn't be allowed.
What is or is there a best way to achieve this in rails?
You need first to check if your page window is the same as parent window if not then your page inside an iframe:
function inIframe () {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}
Then if not you can remove everything in DOM:
// Pure JS something like
var myNode = document.getElementById("foo");
myNode.innerHTML = '';
// jQuery
$('html').empty();
OR you can redirect to an empty page that say its not allowed to be viewed outside of iframe:
window.location = "http://www.yourul.com/empty_page";
Referring to
Related
I am working on developing iOS application using Xamarin. I have a
requirement to call c# method from JavaScript inside UIWebView. How could we achieve that?
The following is html content is loading into UIWebView
const string html = #"
<html>
<body onload=""setBarcodeInTextField()"">
<p>Demo calling C# from JavaScript</p>
<button type=""button""
onClick=""CSharp.ScanBarcode('txtBarcode', 'setBarcodeInTextField')"">Scan Barcode
</button>
<input type=""text"" value="""" id=""txtBarcode""/>
<script type=""text/javascript"">
function setBarcodeInTextField() {
alert(""JS"");
}
</script>
</body>
</html>";
Also, i am getting about://(null) in alert message (onload specified on body tag for displaying alert) when UIWebView loads the html content.
One solution to trigger C# method from website shown in WebView compontent is to:
1) Initialize a navigation to a website in your web code, for example
http://scancode/providedBarCode
2) Then you can override a webview method which is called before navigation actually happens. You can intercept a call there with parameters and ignore the actual navigation
Xamarin Forms (Navigating method of WebView)
webview.Navigating += (s, e) =>
{
if (e.Url.StartsWith("http://scancode/"))
{
var parameter = e.Url.Split(new[] { "scancode/" }, StringSplitOptions.None)[1];
// parameter will be providedBarCode
// perform your logic here
// cancel the actual navigation
e.Cancel = true;
}
};
Xamarin iOS (ShouldStartLoad method of UIWebView)
webViewControl.ShouldStartLoad = (webView, request, navType) =>
{
var path = request.Url.AbsoluteString;
if (path.StartsWith("http://scancode/"))
{
var parameter = path.Split(new[] { "scancode/" }, StringSplitOptions.None)[1];
// parameter will be providedBarCode
// perform your logic here
// cancel the actual navigation
return false;
}
return true;
}
I have searched the web for the past hour for a solution. I want to achieve the following hover-behavior on iOS.
1st tap -> hover on
2nd tap -> hover off
This will mainly be used to present images.
Because of the CSS tag I assume you want to do this in HTML.
This is almost impossible to do without javascript. You could add a click event handler that toggles the .hover class. For example (untested code, don't copy-paste ;)
element.addEventListener('click', function(evt) {
if (this.className == 'hover') {
this.className = 'hover';
} else {
this.className = '';
}
});
If you have other classes on the document it's probably easier to use a JS framework, e.g. jQuery (again untested):
jQuery(element).click(function(evt) {
jQuery(this).toggleClass('hover');
});
I'm trying to stop jQuery Mobile hiding the loading spinner when changePage is called.
The program flow goes like this, starting with clicking a link, which has its click event defined like this:
$('body').delegate('.library-link', 'click', function() {
$.mobile.loading( 'show' );
$.mobile.changePage($('#page-library'));
return false;
});
Upon clicking the link, the pagebeforeshow event is fired, which triggers a function to populate the page from the local storage, or else make an ajax call to get the data.
$(document).on('pagebeforeshow', '#page-library', function(event){
ui.populate_data();
});
In ui.populate_data() we get the data from local storage or make an ajax call.
ui.populate_data = function() {
if (localdata) {
// populate some ui on the page
$.mobile.loading( 'hide' );
} else {
// make an ajax call
}
};
If the data is there, we load the data into the container and hide the loading spinner. If not it makes the ajax call, which on complete saves the data in local storage, and calls ui.populate_data()
The problem is, after the pagebeforeshow event is finished, changePage is calling $.mobile.loading( 'hide' ), even though the data might not be there yet. I can't find any way to prevent changePage from hiding the spinner, other than by temporarily redefining $.mobile.loading, which feels pretty wrong:
$('body').delegate('.library-link', 'click', function() {
$.mobile.loading( 'show' );
loading_fn = $.mobile.loading;
$.mobile.loading = function() { return; };
$.mobile.changePage($('#page-library'), {showLoadMsg: false});
return false;
});
and before hiding the spinner in my ui function:
ui.populate_data = function() {
if (localdata) {
// populate some ui on the page
if (typeof loading_fn === 'function') {
$.mobile.loading = loading_fn;
}
$.mobile.loading( 'hide' );
} else {
// make an ajax call
}
};
Surely there must be a way to get complete control over the showing and hiding of the loading widget, but I can't find it. I tried passing {showLoadMsg: false} to changePage, but as suggested by the docs it only does things when loading pages over ajax, which I'm not doing.
Maybe it's too much for many, but I found a solution other than the written in the comments (which didn't work for me).
I use the jquery mobile router and in the 'show' event of a page, I do $.mobile.loading("show");, so when the page appears it does with the loading spinner showing.
Though to hide the spinner, I had to use $('.ui-loader').hide();, which is weird, I know...
I use Jquery Mobile Router for a lot more, but it solved this issue.
(Maybe just listening to the proper event and triggering the spinner would also work, as this is what JQMR does...)
I'm using JQM 1.4.2...
Am using dataprovider object to show the list with 25 records at a time and instead of pagination, i want to show next page at end of scroll using the following code.
But is there any better way instead of window.location.href
if not, is there any option to show the loading message till the page loads.
$(document).ready(function()
{
$('#content').bind('scroll', function ()
{
totalDivHeight = eval($('#content')[0].scrollHeight) - eval($('#content').height());
scrollPosition =eval($('#content').scrollTop())+25;// + eval($('#content').height());
if (scrollPosition >= totalDivHeight)
{
nextPageID = eval("<?= $nextPageID; ?>");
prevPageID = eval("<?= $prevPageID; ?>");
totalPages = eval("<?= $totalPages; ?>");
if (nextPageID < totalPages)
window.location.href='<?php echo $url;?>'+'&Store_page='+nextPageID;
}
});
});
Edit: i tried ajax function instead of window.location.href but i dont know how to send the pagenumber variable to the provider?
You are probably looking to lazy load content and attach to the end of the table and you do that via ajax.
You should not reinvent the wheel others already have done this, just one of the implementations is here:
http://dcarrith.github.com/jquery.mobile.lazyloader/
Edited to add the solution suggested by #alistair-laing.)
After reading this post reply by #jek, I could make multiple links on my page that would pass an id variable through the URL so that the content of the dialog could be loaded in on the fly. However, I really want this to be a modal dialog:
(edited to include the fix; nb: the original script was in the post linked to above, all I did was break it)
$(function (){
$('a.ajax').click(function() {
var url = this.href;
var dialog = $('<div style="display:none"></div>')
.appendTo('body')
// load remote content
dialog.load(
url,
{},
function (responseText, textStatus, XMLHttpRequest) {
dialog.dialog({
modal: true,
width: 500
});
}
);
//prevent the browser to follow the link
return false;
});
});
quiet a few things. Try move the content from your first .dialog into your second .dialog which you call as part of the the .load callback. What youare doing is creating dialog then injecting content into it only to call it again. You could also remove the the autoOpen so that the dialog opens with the content.