Grails groovy - render pure js code - javascript

this is a duplicate of Executing groovy statements in JavaScript sources in Grails
with one difference, i just want to render the js-code, no script tags around it
imagine someone loads a script from my server inside his html like
<script type="text/javascript" src="mywebsite.com/xyJs/xyz"></script>
and i want to render the "content" of the request
inside the controller i parse the /xyz and find out what must be inside the script and an absolute link for the src of iframe
i need to run this groovy script too
i´m trying to do this like
render "document.writeln('<iframe src=\"${grailsApplication.config.grails.serverURL}/superStuff/stuff23/${profil.slugs}\" name=\"SuperStuff\" width=\"300\" height=\"600\" align=\"left\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" frameborder=\"0\" ></iframe>');"
but the browser just ignores the document.writeln('
and treat the iframe like an html element and renders its content
how can i deklare that its just a tiny bit of js-code without setting script tags?
i also tried to render a view like
render(view:'SuperStuff',model:[profil:profil])
and inside the view SuperStuff is
<%# page contentType="text/javascript; UTF-8" %>
document.writeln("<iframe src='${grailsApplication.config.grails.serverURL}/superStuff/stuff23/${profil.slugs}' name='SuperStuff' width='300' height='600' align='left' scrolling='no' marginheight='0' marginwidth='0' frameborder='0' ></iframe>");
but this also doesnt work and renders the content of the iframes´s src
what i doing wrong ?
for any hint, this makes me fuzzy, thanks in advance

yeaaaah got that one fixed
render contentType: 'text/javascript', text: "document.writeln('<iframe src=\"${grailsApp......."

I believe, your controller works just as expected: it returns the javascript-code exactly as you want: your call to document.writeln(). Please use the Network-Panel of Firebug, Chrome DevTools or similar to verify this.
However, the js-code will immediately be executed which results in the inclusion of the specified iframe-element on the current position, as explained for example here.

Related

Embed object to display PDF showing up as invisible

I am using Javascript to input an embed tag to my HTML to display a pdf. The object takes up space but doesn't display anything and is essentially invisible. I tried putting it inside an object tag as well but it doesn't work.
// Invisible
var pdfObj = document.createElement("embed");
pdfObj.setAttribute("src", "./test.pdf");
content?.appendChild(pdfObj);
The pdf file does exist and when I simply put this code in the HTML it displays fine but doesn't when I use javascript.
// works fine
<div class="content" id="main_div">
<embed src="../test.pdf" width="500" height="375" />
</div>
Here is how it shows on the HTML when I use Javascript:
Embed Screenshot
Thanks!
Firstly, welcome to Stackoverflow. You need to make clear pathway in javascript.
`pdfObj.setAttribute("src", "./test.pdf");` this is your code.
`pdfObj.setAttribute("src", "../test.pdf");` this is what it needs to be.
and
try firstly create the element. You just gave a name to document.createElement("embed"). So firstly create it then setAttribute. After that it will work
If this won't work let me hear. I will help as possible as i can

I want to include a certain div from another url into my site

I have this
<div>
<object type="text/html" data="https://www.urlIWantTheDivFrom.com"
width="800px" height="600px">
</object>
/div>
However this gives me the whole webpage. I want to copy a certain div. I also tried some js. However I cannot find out how to copy the exact div as well
document.getElementById("div").innerHTML='<object type="text/html" data="https://www.urlIWantTheDivFrom.com" ></object>';
Embedding another webpage content is not allowed by default, but here is a thing which you may try using jQuery ajax:
$.get('https://www.urlIWantTheDivFrom.com', function(pageText){
// Appends to your div
$('#myDiv').append(
$(pageText) // parses returned html
.find('#externalDiv') // gets it div
);
})
But remember that online content publisher still has rights over his content, also remember that some tags link may be broken after importing.

PHP saveHTML function is not saving HTML properly

I have been trying to save the source code of a section of a webpage using PHP. When I extract the content of whole webpage, the source code order is preserved but when I try to get part of the document using
$dom = new DOMDocument;
$dom->loadHTML($webpage);
$xpath = new DOMXPath($dom);
$query_tag = "//div[contains(#class, 'class-name')]";
$result = $dom->saveHTML($xpath->query($query_tag)->item(0));
The script tag gets messed up. Until now, this is the only website where this issue occurred. Are there some limitations of saveHTML function that I am not aware of?
This is what I should be receiving:
<div id="sponsored-category-header" class="page-header sponsored-category-header clear"> <script type="text/javascript">jQuery(document).ready(function($) {
var cat_head_params = {"sponsor":"SEO PowerSuite","sponsor_logo":"https:\/\/www.searchenginejournal.com\/wp-content\/plugins\/abm-sej\/includes\/category-images\/SPS_128.png","sponsor_text":"<div class=\"taxonomy-description\">Dominate Google local search results with ease! Get your copy of SEO PowerSuite and keep <a rel=\"nofollow\" href=\"http:\/\/sejr.nl\/PowerSuite-2016-5\" onClick=\"__gaTracker('send', 'event', 'Sponsored Category Click Var 1', 'Local Search', 'SEO PowerSuite');\" target=\"_blank\">your local SEO strategy<\/a> up to par.<\/div>","logo_url":"http:\/\/sejr.nl\/PowerSuite-2016-5","ga_labels":["Local Search","SEO PowerSuite"]}
$('#sponsored-category-header').append('<div class="sponsored-category-logo"></div>');
$('#sponsored-category-header .sponsored-category-logo').append(' <a rel="nofollow" href="'+cat_head_params.logo_url+'" onClick="__gaTracker(\'send\', \'event\', \'Sponsored Category Click Var 1\', \''+cat_head_params.ga_labels[0]+'\', \''+cat_head_params.ga_labels[0]+'\');" target="_blank"><img class="nopin" src="'+cat_head_params.sponsor_logo+'" width="96" height="96" /></a>');
$('#sponsored-category-header').append('<div class="sponsored-category-details"></div>');
$('#sponsored-category-header .sponsored-category-details').append('<h3 class="page-title sponsored-category-title">'+cat_head_params.sponsor+'</h3>');
$('#sponsored-category-header .sponsored-category-details').append(cat_head_params.sponsor_text);
});</script> </div>
This is what I actually get:
<div id="sponsored-category-header" class="page-header sponsored-category-header clear"> <script type="text/javascript">jQuery(document).ready(function($) {
var cat_head_params = {"sponsor":"SEO PowerSuite","sponsor_logo":"https:\/\/www.searchenginejournal.com\/wp-content\/plugins\/abm-sej\/includes\/category-images\/SPS_128.png","sponsor_text":"<div class=\"taxonomy-description\">Dominate Google local search results with ease! Get your copy of SEO PowerSuite and keep <a rel=\"nofollow\" href=\"http:\/\/sejr.nl\/PowerSuite-2016-5\" onClick=\"__gaTracker('send', 'event', 'Sponsored Category Click Var 1', 'Local Search', 'SEO PowerSuite');\" target=\"_blank\">your local SEO strategy<\/a> up to par.<\/div>","logo_url":"http:\/\/sejr.nl\/PowerSuite-2016-5","ga_labels":["Local Search","SEO PowerSuite"]}
$('#sponsored-category-header').append('<div class="sponsored-category-logo"></script>
</div>');
$('#sponsored-category-header .sponsored-category-logo').append(' <a rel="nofollow" href="'+cat_head_params.logo_url+'" onclick="__gaTracker(\'send\', \'event\', \'Sponsored Category Click Var 1\', \''+cat_head_params.ga_labels[0]+'\', \''+cat_head_params.ga_labels[0]+'\');" target="_blank"><img class="nopin" src="'+cat_head_params.sponsor_logo+'" width="96" height="96"></a>');
$('#sponsored-category-header').append('<div class="sponsored-category-details"></div>');
$('#sponsored-category-header .sponsored-category-details').append('<h3 class="page-title sponsored-category-title">'+cat_head_params.sponsor+'</h3>');
$('#sponsored-category-header .sponsored-category-details').append(cat_head_params.sponsor_text);
}); </div>
In case you missed it, the ending script tag has moved up a few lines.
Just to be clear, I am not talking about rendered HTML. I am talking about the actual source code that I get after making the request. Any help on how to resolve this issue will be appreciated.
I know that the function saveHTML is causing the issue because when I echo the whole page through PHP, every tag is in the right place.
First of all, your code should be triggering a good bunch of warnings like these:
Warning: DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' in
Entity Warning: DOMDocument::loadHTML(): Unexpected end tag :
strong in Entity Warning: DOMDocument::loadHTML(): Tag header
invalid in Entity
This is to expect with on-the-wild HTML (and this page's code is nor particularly bad) but you haven't even mentioned it, what makes me suspect that you might not have error reporting enabled in your development box.
Additionally, the page has huge amounts of JavaScript and DOMDocument is just an HTML parser.
With that, we can get a clear picture of what's happening. Since DOMDocument is not a full-fledged browser it doesn't understand JavaScript code. That means that it detects the <script> tag but it doesn't handle its contents as JavaScript—it merely looks for a closing tag and the first one he finds is this:
$('#sponsored-category-header').append('<div class="sponsored-category-logo"></div>');
^^^^^^
It doesn't know that it's a JavaScript string and should be ignored. Instead, it thinks the wrong tag is being closed so it attempts to fix what's technically invalid HTML and adds the missing </script> tag.
For this precise reason, the <script>...</script> tag set has traditionally been written this way:
<script type="text/javascript"><!--
var foo = '<p>Escaped end tag<\/p>';
//--></script>
... so user agents that are unaware of JavaScript can safely ignore the whole tag (hey, it's nothing but a good old HTML comment). However, nowadays it's almost universally considered bad practice because "all browsers understand JavaScript".
Final note: the DOM extension is probably aware of the <script> tag and knows it isn't allowed to have other tags inside. That explains why inner opening tags are not considered.

How to call a PHP function or a file on image click in ZendFramework 2?

The problem I am having is similar to some questions already posted, but none of them provids an answer in respect to ZF2.
What I am trying to achieve is this: on an image click a clearbox (lightbox) should open with a form from a different PHP file.
What i have:
a view script in .phtml format which contains the image to be clicked.
an image.php file which contains the code for the function to be executed when the form is processed in the clearbox.
clearbox is attached and working fine:
<a
href="link\to\image.php"
rel="clearbox[width=800,height=600]"
title="Caption"
>
<img
src="img/test.jpg"
class="img-responsive img-rounded"
style="width:150px; height:200px;"
>
</a>
The problem:
First of all, where should I keep the image.php file in ZF2? And if I need to write a function as we do in pure PHP, keep all the functions in one file, whats the best practice in ZF2?
I tested the file as separately and it worked, but if I use the same URL for the clearbox, it will also display the layout for the page in box, which I don't want. So I need just the content of the file to work.
Please, let me know if the question is unclear.
EDIT:
Also, the page used in clearbox is coming through router using a separate .phtml view script, it would be great if someone can tell me how to call the file directly for the clearbox.
the solution for your problem is very simple you need to tell ZF2 that your view model is terminal so it won't render the layout file:
<a href="link\to\image.php?terminal=true" ...
and in the action :
$isTerminal = $this->params()->fromQuery('terminal',false);
$view = new ViewModel();
$view->setTerminal($isTerminal);
$view->setTemplate('my view script');
$view->setVariables(array(
'myvar' => $myvar,
));
return $view;

Passing Javascript To a Redirected Web page

The code I want to run upon triggeting the redirect, is to go to another web page (or local html file, either is possible in this situation), however pass some javascript to run on that page, as that page works off embeding content in Iframes. This needs to be done to allow me to specify the content in the iframe upon redirect.
To put it simpler. How can I make it so when you go to website.com/about/, it redirects to website.com/ with the content for /about/ loaded in an iframe?
<head>
<title> CodeBundle </title>
<script>
function home() {document.getElementById("loadedpage").src="home.html";}
function about() {document.getElementById("loadedpage").src="about.html";}
function reviews() {document.getElementById("loadedpage").src="reviews.html";}
function tutorials() {document.getElementById("loadedpage").src="tutorials.html";}
function blog() {document.getElementById("loadedpage").src="blog.html";}
</script>
</head>
<body>
<header>
<br><hr><font size=27><a onClick="home();">Code Bundle</a></font><br><hr>
<div ALIGN=RIGHT>
<font size=6> | <a onClick="about();">About</a> | <a onClick="reviews();">Reviews</a> | <a onClick="tutorials();">Tutorials</a> | <a onClick="blog();">Blog<a> |</font> <hr>
</div>
<iframe id="loadedpage" src=home.html width=100% height=100% frameborder=0>Iframe Failed to Load</iframe>
</header>
</body>
</body>
this is my index.html for website.com/
I want to write a page so that when you go to website.com/about/ it redirects to website.com/ running the javascript function about(), so as to display the about page.
You will have to either pass some data using a query parameter or a fragment identifier.
See:
http://en.wikipedia.org/wiki/Query_string
http://en.wikipedia.org/wiki/Fragment_identifier
In either case you will have something present in the url and it will look like:
http://www.example.com/?page=about
or:
http://www.example.com/#about
or - this would be best:
http://www.example.com/#!/about
because it could let you make the website crawlable. See:
Making AJAX Applications Crawlable
Now after reading your comment to the answer by theredled that you "add new content regularly and loading that in embeded iframes is quicker than writing new html every time" I have to ask this: aren't you using a templating system in your website?
Keep in mind that making AJAX-loaded content and using fragment identifiers to display the right content is not done because the page creation is easier (it isn't) but because the user experience is faster and more responsive. See for example the website for the SoundJS library:
http://www.createjs.com/#!/SoundJS
When you click the link to PreloadJS at the top you go to:
http://www.createjs.com/#!/PreloadJS
The content is reloaded, the address bar changes, but the page is actually not reloaded. (You can see that it is properly crawlable because it shows in the results if you google for ReloadJS.)
Pass content by a user session ?
However, it's a quite dirty case, maybe you already know that :)

Categories

Resources