Problem viewing a div with a simple function - javascript

I have a problem I would say stupid with html and javascript, a simple function that should make me visible and invisible a div crashes the html making it empty!
<button onclick="open()">Modifica</button>
js:
var x = document.getElementById ("joseph");
if (x.hidden == false) {
x.hidden = true;
} else {
x.hidden = false;
}
before:
after:

You don't have to write pre-defined methods. It works same for all other tech stacks such as Mysql, PHP, etc.
Recommend using opens, not open if you insist to use the open word.
<button onclick="opens()">Modifica</button>

You've hit a variation of this problem.
When searching for a variable named open the browser finds document.open before it finds your open function.
document.open opens a new document for writing (with document.write) which erases the existing document.
window.open is also predefined, so making a new global function with that name is not advised.
As with the linked question: Use addEventListener instead of onclick attributes.

Related

my sapUI5 app only jumps once into my print method

this is my very first question on Stackoverflow. I am currently developing a print function in my sap ui5 app to print out certain UI controls. I've got the function from here: http://embed.plnkr.co/jjyEPa1updkjBiNZqumS/preview
However, during runtime, when I click on the print button, my app only jumps to the method once and executes it correctly (to print). But after that, I can press the printbutton as often as I want, nothing happens and I can't find out why.
what the method does: i replace the body with a temporary body, which only contains the elements to be printed and execute window.print(). afterwards i insert the original body content again. Of course I use the UI controls to grab the HTML tags.
onPrintChart: function(oEvent){
var oTarget = this.getView(),
sTargetId = oEvent.getSource().data("targetId");
if (sTargetId) {
oTarget = oTarget.byId(sTargetId);
}
if (oTarget) {
var $domTarget = oTarget.$()[0],
sTargetContent = $domTarget.innerHTML,
sOriginalContent = $(document.body)[0].innerHTML;
$(document.body)[0].innerHTML = sTargetContent;
window.print();
$(document.body)[0].innerHTML = sOriginalContent;
} else {
jQuery.sap.log.error("onPrint needs a valid target container [view|data:targetId=\"SID\"]");
}
}
I managed to do it in a different, more elegant way without using a temporary body. I used CSS to hide all irrelevant elements (display: none) and keep only the relevant element for printing.
Apparently ui5 hung up when replacing the original body temporarily with another body. I noticed that ALL buttons didn't work anymore, not only the print button.

Button not showing in Plone, onclick also not working

I am editing a plone page to open an Excel document on a specific sheet. I created two buttons to see if either would appear as actual buttons and use the JS function I reference. With this code the exact part of the page looks like the image below.
Why is only text showing instead of the button and why is the onclick attribute not working?
Note: I have changed to links to the spreadsheet for posting it on here but the link has been tested on other webpages
<script type="text/javascript">
function Open_Excel_File(path,sheet)
{
fso = new ActiveXObject("Scripting.FileSystemObject");
if (!fso.FileExists(path))
alert("Cannot open file.\nFile '" + path + "' doesn't exist.");
else
{
var myApp = new ActiveXObject("Excel.Application");
if (myApp != null)
{
myApp.visible = true;
Book = myApp.workbooks.open(path);
var excel_sheet = Book.Worksheets(sheet).Activate;
myApp.range(f_range).Select;
}
else {
alert ("Cannot open Excel application");
}
}
}
</script>
<div>
<button onclick='Open_Excel_File("file://///fs-01\Departments\Underwriting\Statistical%20Data%20and%20Medical%20Information\Statistics\Cancers\Cancer%20Statistics%\Cancer%20Statistics%.xlsx", "Vulvar Ca");'>Open File</button>
<input type="button" onclick="Open_Excel_File('file://///fs-01\deps\uw\stat%20Data%20and%20Medical%20Information\Statistics\Cancers\Cancer%20Statistics%202018\Cancer%20Statistics%.xlsx', 'VCA');'>OPEN FILE</input>
</div>
your onclick value is not a function, it is the result of a function call. Try to change that to onclick="Open_Excel_File"; You'll have to provide the file path at some point
Accessing file system from browser is super restricted for security matters, the only way I see fit is to have a file input and using what user provides
Also Plone filter out a bounce of potential "nasty" tags through a specific configurable tool.
It seems to me that you have injected the in the source HTML of a Page (document) type.
If so, you will see in your browser that in, the page source code, the script tag has been totally stripped away.
So,
a correct way to inject some js in your page, is to load it as portal_javascript resource (plone<=4) or in resource_registry (plone>=5).
tha nasty way is to access, in the ZMI, at https://yourseite:8080/Plone/portal_transforms/safe_html/ and configure it to accept script tags inside a document (all document in your site actually).
If this answer does not satisfy you try to ask in the official community:
http://community.plone.org
hth,
alessandro

Click one of contact in contact list in whatsapp web (javascript)

I try to use JavaScript to perform a click action to click a contact form contact list in order to open a chat in Whatsapp Web.
I use normal click action to click but not working, like
var div = document.querySelector('.infinite-list-item')[0];
div.click();
And i know whatsapp web is made by react js, ant special to perform a click?
I even use jQuery click() function but still not working, what should I use?
You're using document.querySelector() and then trying to access it as if it was a data structure.
document.querySelector() will return only one DOM element. You should try using document.querySelectorAll()[0] if you want to put all the contacts in a structure.
Solution:
var itemList = document.querySelectorAll('.infinite-list-item');
itemList[0].click();
Also, this would work as an alternative solution ( though using querySelector instead of querySelectorAll to target a class name that is used more than once is not at all recommended ):
var div = document.querySelector('.infinite-list-item');
div.click();
document.queryselectorAll("div span").forEach((/*YourElement*/, index) =>{
setTimeout(()=>{
/*here should go a validation so that it only runs on the
contacts you want, but i don't write it because that's not the
main idea*/
//¡¡¡¡the important part is this!!!!!
function simulateMouseEvents (elemento, eventName) {
var mouseEvent = document.createEvent ('MouseEvents');
mouseEvent.initEvent (eventName, true, true);
/*YourElement*/.dispatchEvent (mouseEvent);
}
simulateMouseEvents ('enlace', "mousedown");
},index*20)
}
/this code snippet solved my problem in a personal project ... I am enclosing image of the project(unfinished so it may have bugs) in case it helps you understand better/
Image of the proyect

Translating an an 'on.click' event in PHP, from 'window.open' to a basic URL link

I'm trying to perform a hack on a Joomla component to better suit my needs. The current code looks like this:
$eventprnt = 'onclick="window.open(\'index.php?option=com_coupon&view=coupons&task=print_coupon&id='.$item->id.'\')" ';
$eventcart = ' onclick="add_to_cart('.$item->id.');"';
$live_prt = 'coupon_print';$live_crt = 'coupon_cart';
if($item->coupon_type == 4){
$dealevent = 'onclick="prompt_link('.$item->id.','.$final_price.',this)"';
}
The $eventprnt function currently opens a browser window. I would like to convert this to a standard URL Link whilst retaining the onclick event (ie: Not swapping to echo with <a> or using a JScript <button> (There are multiple calls, already attached to this trigger).
I've tried everything, the syntax is killing me. - I've tried window.location instead of window.open, but to no avail.
location is the way to go:
$eventprnt = 'onclick="location.href=\'index.php?option=com_coupon&view=coupons&task=print_coupon&id='.$item->id.'\';" ';

AIR HTMLLoader window.open is not working

I have a web project developed in Flex which I have to make work standalone using AIR.
I created Air project and loaded the web application using flash.html.HTMLLoader.
The content is loading fine and working.
There are few buttons which open different links using javascript functions window.open.
The links are not opening. The javascript function is getting called using ExternalInterface and I placed alerts in that which is displaying.
The function contains simple window.open
window.open("http://www.google.co.in","Google");
I tried several solutions mentioned but none of them are working.
http://digitaldumptruck.jotabout.com/?p=672
http://soenkerohde.com/2008/09/air-html-with-_blank-links/
http://cookbooks.adobe.com/index.cfm?event=showdetails&postId=9243
I even tried loading a simple page in HTMLLoader component with window.open method still it is not working. On button click only alert is working but window.open is not opening the link.
<html>
<head>
<title>Test</title>
<body scroll="no">
<input type="button" value="Click" onClick="window.open('http://www.google.co.in');">
</body>
</html>
Could some one help me please
This is a radical suggestion that may or may not work, but I think it's worth a try.
Override the window.open method itself
As before, wait until the Event.COMPLETE is fired, then take it from there:
var html:HTMLLoader = new HTMLLoader();
var urlReq:URLRequest = new URLRequest("whatever.html");
var oldWindowOpen:Object; // save it, just in case
html.load(urlReq);
html.addEventListener(Event.COMPLETE,
function (event:Event):void {
oldWindowOpen = html.window.open;
html.window.open = asWindowOpen;
});
function asWindowOpen(href:String, name:String="_blank", specs:Object=null, replace:Object=null):void {
var urlReq = new air.URLRequest(href);
air.navigateToURL(urlReq);
}
You should probably fill out some of the function to handle the other inputs as specified in the W3Schools Reference for Window open() Method. You may have to (or want to) change all the parameter types to Object, just to be safe, since, unlike ExternalInterface interactions, the JavaScript-ActionScript types are not automatically typecast across the AIR-WebKit exchange.
The AIR Webkit environment is quite restrictive in its support for the window.open method. See Adobe documentation on Restrictions on calling the JavaScript window.open() method.
The easiest way to deal with this is just let the system's default browser open the links. Adobe documents this very question, and shows how you can pop open url's from within AIR:
var url = "http://www.adobe.com";
var urlReq = new air.URLRequest(url);
air.navigateToURL(urlReq);
Generalizing this:
function openExternalLink(href:String):void {
var urlReq = new air.URLRequest(href);
air.navigateToURL(urlReq);
}
One option: Assuming you're running jQuery on the page, you could have all the links open externally as so:
var html:HTMLLoader = new HTMLLoader();
var urlReq:URLRequest = new URLRequest("whatever.html");
html.load(urlReq);
html.addEventListener(Event.COMPLETE,
function completeHandler(event:Event):void {
html.window.jQuery('a').click(clickHandler);
});
function clickHandler( e:Object ):void {
if (e.target && e.target.href) {
openExternalLink(e.target.href);
}
e.preventDefault();
}
For more on handling DOM Events in ActionScript, see the relevant Adobe Documentation.
None of that is tested, but hopefully it gives a rough outline.
Otherwise, if you are trying to do something fancy, like pop up AIR windows with HTMLLoader frames in them, I did find one blog post discussing that: Opening links in AIR’s HTML loader

Categories