When I use the iframe in VSCode's WebView, popups are disabled by default. That is if you have an anchor tag with target="_black", it will not open a new browser.
Is there any way to resolve this problem?
const panel = vscode.window.createWebviewPanel('openWebview',
'Title',
vscode.ViewColumn.One,
{
enableScripts: true
});
panel.webview.html = getWebviewContent();
....
....
function getWebviewContent() {
return `<iframe src="http://localhost:8080/homepage" title="Title"></iframe>`;
}
The homepage has some external references which use the anchor tag
OWASP
If I click this link within the iFrame nothing happens. I wanted to open the link in an external web browser.
Thanks you
I did not find a straight solution. There is a restriction in the VS code Webview. Hence, the only solution I can think about is to add a message support in your html page.
Basically, when you click on a link we will send a message event to the parent frame which can be captured in the in the WebView.
For example, HTML page should be updated as
<a href="https://owasp.org" rel="noopener noreferrer" target="_blank"
onClick="window.parent.postMessage(this.href, '*')">OWASP</a>
And in VS code plugin,
const panel = vscode.window.createWebviewPanel('openWebview',
'Title', vscode.ViewColumn.One, { enableScripts: true });
panel.webview.onDidReceiveMessage(
message => {
switch (message.command) {
case 'show':
vscode.env.openExternal(vscode.Uri.parse(message.text));
return;
}
},
undefined,
context.subscriptions
);
panel.webview.html = getWebviewContent();
});
...
...
function getWebviewContent() {
return `<script>
window.addEventListener('message', function (e) {
const vscode = acquireVsCodeApi();
vscode.postMessage({command: 'show', text: e.data});
}, false);
</script>
<iframe src=" http://localhost:8080/homepage" ></iframe>`;
}
Related
I am using electron in building Desktop application when I am clicking on any button it make refresh for the whole application , I need to prevent this refresh
<script>
window.$ = window.jQuery = require('jquery')
$(function(){
var config = require('../jsonFiles/pathData.json');
$("#aspnetCorePathRename").val(config.AspnetCorePath);
$("#angularPathRename").val(config.AngularPath);
});
function ren(e) {
const fs = require("fs");
fs.readFile('./scrips/Rename.ps1', function (err, data) {
if (err) {
$(".Console").html(err.toString());
alert(err.toString());
return console.error(err);
}
alert(data.toString());
});
readFile();
}
</script>
That's most likely not a Problem of electron or your Script, but of the way you defined the buttons in your HTML. If you define it like this:
<input type="submit">
HTML assumes it's send button for a form and will refresh the page. If you define it as button
<button type='button'>Submit</button>
the page will not be refreshed.
If you have to define it as input I'd assume you prevent the default behaviour with jQuery like this:
$("input[type='submit']").click(function() { return false; });
I am trying to get a jquery ui dialog to open in a parent window from a child window. In other words, as the child can only have the dialog popup only with in its window's constraints, I would like to be able to use the contents of the dialog's popup in the constraints of the parent window.
I was trying to use a window.opener to tie in a link, but wasn't having much luck.
Here's the code that opens the window from a Backbone View HTML page:
popoutWindow = window.open("campaign/genericPopout.aspx", "search", "width=800, height=600, toolbar=0, status=0, menubar=0, scrollbars=1, resizable=1", true);
inside the genericPopout.apsx's call to a file that builds, with Backbone View, with this command,
new popupSearchView();
it calls the following Backbone View:
var popupSearchView = Backbone.View.extend({
events: {
"click #closeBtn" : "closePopupSerachView"
},
initialize: function() {
this.template = _.template("<div id='searchPopupView'>" +
"<div style='width: 100%; text-align: center;'>Hello World</div>" +
"<div style='text-align: center;'><input type='button' id='closeBtn' value='Close' /></div>" +
"</div>"
);
this.render();
},
render: function () {
formString = this.template();
$(formString).dialog({
autoOpen: true,
height:460,
width: 350,
title: "Search",
modal: false
})
return this;
},
closePopupSearchView: function () {
$(this).dialog("close");
}
});
It opens the popup dialog fine within the newly opened window, but it doesn't seem to work. In the parent window HTML, I have a div devoted to this dialog:
<div id="popupSearchViewDiv"></div>
and I tried setting the Backbone el to this div by this:
el: $("popupSearchViewDiv", window.opener.document)
yet this does not seem to work. Any thoughts on how I can accomplish this?
Thank You
I came up with a way to do this. I'm not sure how efficient it is, but it may be the only way.
In the opener window, I placed the code jquery-ui dialog:
function openSearchViewPopup() {
$("#popupSearchViewDiv").dialog({
autoOpen: true,
height:460,
width: 350,
title: "Search",
modal: false
}
With a predefined DIV in the index.aspx of:
<div id="popupSearchViewDiv"></div>
From the window opened from the index.aspx that holds the above function code, I call the function like this:
render: function () {
var htmlToWrite = this.template();
$(window.opener.docuemnt.getElementById("popupSearchViewDiv").append(htmlToWrite);
$window.opener.openSearchViewPopup();
}
This seems to allow the jquery-ui dialog popup to open in the window.opener window and to be used there.
I would like to user the beautiful jQuery Mobile dialog:
Open dialog
To ask a user if he really wants to submit a form.
My idea is: user fills the form then clicks on submit and so he sees a dialog like "do you really want to submit the form? Yes/No" and if yes the form is sent using ajax to my insert.php page if no the dialog is simply closed.
I've read in the official documentation and it looks like this is simply a graphical tweak to show any #page of my jqm web app like a dialog.
How can I use this dialog page as confirm / cancel of my form submission?
To be clearer: I don't want to use standard javascript like:
function insertClienti()
{
$('#form').submit(function(e) {
if(!confirm('really submit the form?')) {
e.preventDefault();
return;
}
$.post("insert.php", $("#form").serialize());
});
}
but only jQuery Mobile's dialog.
Thanks for your time and patience.
I was simply using the wrong jQuery Mobile component.
Popup dialogs are simply made for this and are very easy to use.
The documentation is here:
http://jquerymobile.com/demos/1.2.0-alpha.1/docs/pages/popup/
Here's the code I used in my app (I use bassistance validator):
$(document).bind('pageinit', function () {
validaFormInserimento();
});
function insertClienti()
{
$.post("insert.php", $("#form_inserimento_clienti").serialize());
}
function validaFormInserimento()
{
$("#form_inserimento_clienti").validate({
rules: {
nick: "required"
},
messages: {
nick: "Campo obbligatorio"
},
errorPlacement: function(error, element){
if (element.attr("name") === "nick") {
error.insertAfter($(element).parent());
} else {
error.insertAfter(element);
}
},
submitHandler: function(form){
$( "#popupDialog" ).popup( "open" );
}
});
}
function disabilitaSubmit()
{
$('input,select').keypress(function(event) { return event.keyCode != 13; });
}
function insertClienti()
{
$.post("insert.php", $("#form_inserimento_clienti").serialize());
$( "#popupDialog" ).popup( "close" );
$("#form_inserimento_clienti").each(function() {
this.reset();
});
}
I wrote like there: http://jqueryui.com/demos/tabs/#...follow_a_tab.27s_URL_instead_of_loading_its_content_via_ajax
<script type="text/javascript">
$(function(){
$("#tabs").tabs({
select: function(event, ui) {
var url = $.data(ui.tab, 'load.tabs');
if( url ) {
location.href = url;
return false;
}
return true;
}
});
});
</script>
<div id="tabs">
<ul>
<li>
Home
</li>
<li>
About
</li>
</ul>
</div>
Tabs are created, but initial list (div, ul, li) is visible as well. Another problem: when I hover over tab, I see URL kind of /default.htm#ui-tabs-1, /default.htm#ui-tabs-2 etc. But I want to see URL "/default.htm" over the 1st tab and URL "/about.htm" over the 2nd tab.
What could I do to solve my problem?
UPDATE
In version 1.9 there is powerful widget "menu".
You are miss interpreting the jQuery UI Tabs.
This Tabs are for having content hide/show and if using ajax pull the page info and show it on demand.
if you want those tabs to act as a menu ... then you need a menu, not the jQuery UI Tabs.
If your idea if to use this tabs but to fetch the /about.htm as a new content, then you can use the ajax example
http://jqueryui.com/demos/tabs/#ajax
keep in mind that it will fetch the entire content, so the /about.htm page should not have <html> neither <body> tags
I don't want to encourage you to do this, but the solution is currently available on jQuery UI's website: http://jqueryui.com/demos/tabs/#...follow_a_tab.27s_URL_instead_of_loading_its_content_via_ajax
You may extend it a bit to follow only certain URLs:
select: function(e, ui)
{
var tab = $(ui.tab);
var url = $.data(ui.tab, 'load.tabs');
if(url && tab.attr('data-ajax') == 'false')
{
location.href = url;
return false;
}
return true;
}
Then, when defining tabs:
<li>...</li>
I would like to prevent the default behaviour of a click on a link. I tried the return false; also javascript:void(0); in the href attribute but it doesn’t seem to work. It works fine in Firefox, but not in Chrome and IE.
I have a single tab that loads via AJAX the content which is a simple link.
<script type="text/javascript">
$(function() {
$("#tabs").tabs({
ajaxOptions: {
error: function(xhr, status, index, anchor) {
$(anchor.hash).html("Couldn't load this tab. We'll try to fix this as soon as possible. If this wouldn't be a demo.");
},
success: function() {
alert('hello');
$('#lk').click(function(event) {
alert('Click Me');
event.preventDefault();
return false;
});
}
},
load: function(event, ui) {
$('a', ui.panel).click(function(event) {
$(ui.panel).load(this.href);
event.preventDefault();
return false;
});
}
});
});
</script>
<body>
<div id="tabs">
<ul>
<li>Link</li>
</ul>
</div>
</body>
The content of linkChild.htm is
Click Me
So basically when the tab content is loaded with success, a click event is attached to the link “lk”. When I click on the link, the alert is displayed but then link disappears. I check the HTML and the element is actually removed from the DOM.
$('#selector').click(function(event) {
event.preventDefault();
});
The event object is passed to your click handler by default - you have to have something there to receive it. Once you have the event, you can use jQuery's .preventDefault() method to cancel the link's behavior.
Edit:
Here's the fragment of your code, corrected:
$('a', ui.panel).click(function(event) {
$(ui.panel).load(this.href);
event.preventDefault();
return false;
});
Notice the addition of the word 'event' when creating the anon function (or you could use just e, or anything else - the name is unimportant, the fact there's a var there is.