Adding javascript code to URL - javascript

I' ve written some line of js code to sum some values in a specific page. To use my function, i usually open the target page with Chrome, i open the console,i paste my code and call the function. Is there a way to load page and inject code automatically in the URL or somewhere else?

Just include it in between <script> tags on that page:
<script>
// Your code
</script>
You can place them just before the closing </body> tag.

Like this?
<div>some HTML</div>
<!--add the script to your html file-->
<script src="script/url.js"></script>
<script>
myFunction(); //function call
</script>

You can add a bookmark with this code at the place of the URL:
javascript:(function(){
alert('ok');//place here you're personal script
})();
You can use a userscript:
TamperMonkey for Chrome
GreaseMonkey for Firefox
You can use a browser extension:
https://developer.mozilla.org/en-US/Add-ons
https://developer.chrome.com/extensions

Related

ckeditor: "a is undefined"

I just added CKEditor to my website, but I'm getting the following error in my console:
I followed the installation guide as it's written so I have no idea what's wrong.
Here's, briefly, what my call looks like:
<textarea id="full-editor" name="full-editor" rows="10" columns="6"></textarea>
<script type="text/javascript">
CKEDITOR.replace('#full-editor');
</script>
Aah.. try this
Remove # from the selector inside CKEDITOR.replace('#full-editor');
According to installation guide you shared, this is what u need
CKEDITOR.replace('full-editor'); // NO #. You must have got confused with jQuery
This also happened whenever we put initializer script before <textarea>.
Ensure to put
<script>
CKEDITOR.replace( 'editor1' );
</script>
before the </body> tag (i.e. closing body tag).
<script type="text/javascript">
CKEDITOR.replace('#full-editor');
</script>
Please add above code in external js file and include this js in html page after title page like
$(document).ready(function () {// save as like ckEditorInclude.js
CKEDITOR.replace('#full-editor');
}
<script src="Your folder path/ckEditorInclude.js" type="text/javascript"></script>
This also happened whenever we put initializer script before .
Ensure to put
CKEDITOR.replace( 'editor1' );
before the </body> tag (i.e. closing body tag).
This append to me because i was trying to use CKEDITOR.replace("editor") but there was no element in dom with Id or name "editor"
The issue for me was I copied the code locally from the cdn so that I can work on it if I am not online. I am using version 4.9.2 standard.
Examining the chrome console gave several 404 errors which were not obvious using FireFox. Reverting back to the cdn resolved the issue.
Unfortunately, no working offline with this it seems at least not for this version of ckeditor.

Why does my code in a script element not run?

I have the following script in my smarty template:
<script src="http://code.jquery.com/jquery-latest.js">
$(document).ready(myBookings);
</script>
However it doesn't work on page load. $(document).ready(myBookings); does work when I run it in Mozilla firebug console though. What am I doing wrong?
script elements can have a src attribute or content, but not both. If they have both, the content is ignored (the content is considered "script documentation," not code).
Use another script block for your document-ready handler
<script src="http://code.jquery.com/jquery-latest.js">
</script>
<script>
$(document).ready(myBookings);
</script>

Insert javascript widget on one page of Drupal site

I have a javascript widget that can be inserted on an a plain-old html page like this:
<html>
<head>
<script type='text/javascript' src="http://example.com/widget.js"></script>
</head>
<body>
<script type='text/javascript'>
//<![CDATA[
try{ widget_constructor('key',500,400); }
catch(e){ alert(e.message); }
//]]>
</script>
</body>
</html>
I would like to insert this javascript on one page of a Drupal 6 site (not every page).
This is what I have tried so far to get the script tag in the HEAD:
I set the Full HTML input format to allow php.
For the Drupal page, I set the input format to Full HTML
I then added this to the top of the body of my Drupal page:
<?php
drupal_set_html_head('<script type="text/javascript" src="http://example.com/widget.js"> </script>');
?>
But Drupal doesn't parse the php and instead prints this at the top of the page:
<?php drupal_set_html_head(''); ?>
Any suggestions as to how I can do this?
#Jeff: You should not really reconsider that. Using the PHP Filter is generally a bad idea as it potentially opens security holes. (In case someone would gain access to your user account or you have a misconfiguration in your settings this is a direct path to compromising the whole server)
I would rather propose to add js via the template.php of your theme. Also the right method to use is drupal_add_js with the option inline:
http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_add_js/6
Here are some further reads on how to use it:
http://drupal.org/node/304178#comment-1000798 http://drupal.org/node/482542
I'm not too familiar with Drupal so this answer merely gets around your widget not loading without actually answering the question why that PHP block isn't being parsed as PHP.
You don't need to call the <script> tag inside the <head>. You can just place that line right above the <script> tag that calls the widget constructor.
<html>
<head>
</head>
<body>
<script type='text/javascript' src="http://example.com/widget.js"></script>
<script type='text/javascript'>
//<![CDATA[
try{ widget_constructor('key',500,400); }
catch(e){ alert(e.message); }
//]]>
</script>
</body>
</html>
After writing that all out, I figured out the problem...
The full html input format has an HTML corrector filter turned on by default, and that filter appears to have caused problems with the PHP code. Reordering the filters to put the PHP evaluator first fixed the problem.

Can you make a javascript function replace itself on the page with custom html?

I have some javascript which will create some sort of widget on a page. I will be giving this snippet to clients so I want them to have to do very little.
The most obvious solution which I have work right now looks something like this:
<div id="divContent"></div>
<script type="text/javascript">
MakeWidget('divContent');
</script>
Make widget basically looks for the divContent div and fill it with my widget's html.
Is there a better way to do this?
Can you replace a Script Tag with a Div using Javascript in that Script Tag?
I would really like it if I could reduce the code down to only the MakeWidget function and it would replace itself and the script tag with the html the function generates.
Edit - I essentially want to generate HTML exactly where the MakeWidget function is called on the page.
Can you replace a Script Tag with a Div using Javascript in that Script Tag?
Yes. When the <script> element is reached, assuming it is not a defer or async script, it will be the last script element in the page so far. So you can say, either inline or in an external script:
<script type="text/javascript">
(function() {
var scripts= document.getElementsByTagName('script');
var script= scripts[scripts.length-1];
var div= document.createElement('div');
div.innerHTML= 'Whatever content is going to be inserted';
script.parentNode.insertBefore(div, script);
})();
</script>
You have to have MakeWidget defined somewhere, right? Presumably this is going to be in an external script. Why not just have the external script source just attach itself to the divContent using the window.onload method?
This would result in this code on your client's page:
<script src="http://foo.com/makewidget.js"></script>
Your makewidget.js code could then look like this:
window.onload = function() { MakeWidget('divContent') }
There may some issues with other scripts loading and probably some cross-browser compatibility issues but that should get you pointed in the right direction.
So you want to have a script element which replaces itself with div.
Your initial code is like this:
<div id="divContent"></div>
<script>
MakeWidget('divContent');
</script>
You can rewrite it like this (I am using JQuery):
<script id="scriptContent">
$('#scriptContent').after('<div id="divContent"></div>');
MakeWidget('divContent');
$('#scriptContent').remove();
</script>
I have not tried it though!
I would think it would be possible to do it as follows:
<div id="divWidgets">
<script type="text/javascript">
MakeWidgets("divWidgets");
</script>
</div>
The end result should be (if I understand your description correctly) that MakeWidgets will replace the contents of the DIV, which, in this case, is the script itself.

inline javascript are interrupting the load of the html

I want the html are fully loaded then execute the inline javascript because this script stops the load of the html document.
To try to solve this problem I put the inline javascript code at the end inside a div then i use the jquery .append method
$("#subhimedia").appendTo("#himedia");
This works and appends the inline js that is located inside #subhimedia and takes it inside the #himedia div.
The probblem is that duplicate the #subhimedia div and in internet explorer it freeze the browser.
The inline javascript is:
<!--JavaScript Tag // Tag for network 258: Hi-Media Portugal // Website: Lifecooler // Page: HOME // Placement: HOME_MREC_300x250 (1653713) // created at: Aug 29, 2008 1:35:27 PM-->
<script language="javascript"><!--
document.write('<scr'+'ipt language="javascript1.1" src="http://adserver.adtech.de/addyn|3.0|258|1653713|0|170|ADTECH;loc=100;target=_blank;grp=[group];misc='+new Date().getTime()+'"></scri'+'pt>');
//-->
</script><noscript><img src="http://adserver.adtech.de/adserv|3.0|258|1653713|0|170|ADTECH;loc=300;grp=[group]" border="0" width="300" height="250"></noscript>
<!-- End of JavaScript Tag -->
You could see the url here: http://www.niceoutput.com/jquery/
Thanks in advance for your help
Mário
Since you are using jQuery, wouldn't
$(document).ready(function(){
//your code to execute when HTML is fully loaded
});
work?
use
$(document).ready(function(){
// your code here
});
before writing any jquery codes..

Categories

Resources