how to add javascript inside php file with html content? - javascript

I have a Wordpress plugin. inside this plugin there is a php file.(for example name.php) this php file contains some codes which they are inside this code:
<script type="text/html" id="tmpl-dokan-single-variations">
//codes
</script>
Now I want to add some Javascript codes and I've tried this code:
<script type="text/html" id="tmpl-dokan-single-variations">
<script type="text/javascript">
//mycodes
</script>
//codes
</script>
Also I've tried echo script but it doesn't work either.
Please tell me how can I add some javascript code inside this file?

There is no way to include the raw string </script> inside a script element.
You would need to find some alternative representation of it (such as <!-- TEMPLATE: SCRIPT END TAG --> and then modify the code (which is presumably JavaScript) that reads the value of the <script type="text/html"> element so it can replace that alternative representation with the end tag you actually want.

I dont know what your php file looks like but you can echo your script like this
<?php
echo '
<script>
// your code here
</script>
';
?>

Related

Script tag is not working in SartajPHP Temp File

I am trying to insert JavaScript code into jQuery ready function. But this code is not working ..
<script runas="jsfunctioncode" renderonce="true">
$("#srchbtn").on("click",function(){
$("#form2").attr("action","<?= getEventPath("usersrch"); ?>");
form2_submit('');
});
</script>
Error Message:- Incomplete attributes in script tag.
You forget attribute function in script tag ..
use
<script runas="jsfunctioncode" function="ready" renderonce="true">
$("#srchbtn").on("click",function(){
$("#form2").attr("action","<?php echo getEventPath("usersrch"); ?>");
form2_submit('');
});
</script>

Is there a way to stop `<script>' from loading the same javascript twice in PHP/Javascript/HTML?

Is there a PHP require_once or include_once for <script>? I know <script> is pure HTML, but does PHP or HTML have such a thing?
I would like to prevent javascript from loading twice in the same page.
Example:
<script type="text/javascript" src="js/jquery-0.4.ajaxify.min.js"></script>
Seems like you are looking for this:
http://wonko.com/post/painless_javascript_lazy_loading_with_lazyload
One of the way you can run script tags in php would be
<?php
....Here is php code
?>
... add the script here(<script>....</script>
<?php
?>
Another probable way is :
Using php inside the script tags for example:
<script type="text/javascript">
var alertMsg = '<?php echo $custom_message; ?>';
alert(alertMsg);
</script>
If you are using buttons
echo('<button type="button" onclick="customfunction();">My Button</button>');
<script>
//call your customfunction here
</script>
AN UPDATE TO SOLVE LOADING SCRIPT CONTENTS TWICE
I would suggest use of javascript function which are called to the specific page as they are needed an example would be
Create a file with a .js eg
example.js
here declare your functions you would put in the script tags of a html page
In the page you want to use the <script>
Include the example.js file and you can call the custom functions from there use the obect orientend approach
Check This resource for more info about obect orientend javascrip

How to load php code into html tag DIV using Jquery?

I am trying to load php code into div tag to show the results but the below code doesn't work for.
Can anyone please help me...to resolve this
my code is below:
<html>
<head>
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
$("document").ready(function()
{
$("#myDiv").load("refreshing.php");
}
</script>
</head>
<body>
<div id='myDiv'> </div>
</body>
</html>
Change script tag to this
<script type="text/javascript">
$(document).ready(function()
{
$("#myDiv").load("refreshing.php");
});
</script>
i.e. you're missing ); at the end
Also, document should not have quotes on it
You cannot load php code .load() because as the web server gets the GET request with .php, the request is passed to PHP interpreter which will execute the PHP script and return the result of the php script instead of code. So what you can do is in your PHP script, do a echo of all the code that you want to display. Or, you can define custom rule in your Apache configuration.
You've forgotten the closing bracket:
$("document").ready(function(){
$("#myDiv").load("refreshing.php");
});

Spaces appear after file_get_contents from HTML file

I have 2 files: script.html and index.php.
My purpose is to generate a text (in this case the country of the user) in an HTML file by a JavaScript. This is the code of this HTML file:
script.html
<script language="Javascript" src="http://www.codehelper.io/api/ips/?js"></script>
<script language="Javascript">document.write(codehelper_ip.Country);</script>
And now, I want to take that text to the PHP file to use it as a variable:
index.php
<?php
$content = file_get_contents(script.html);
echo "http://www.example.com/$content/example"
?>
The problem is that the output from that echo is odd:
echo output:
http://www.example.com/ US /example
Notice the spaces before and after the "US". This destroys the whole purpose of the code, of course. I can't have that URL like that.
Any way I can avoid or remove those spaces?
Why is this happening?
My guess: These spaces are being inserted because of the linebreaks of the JavaScript code. If I don't put any linebreak between one <script></script> and the other <script></script> (such as <script language="Javascript" src="http://www.codehelper.io/api/ips/?js"></script><script language="Javascript">document.write(codehelper_ip.Country);</script>, all in one line) then I get http://www.example.com/US /example. I don't know how I could avoid the space after, though.
Note1: I have already tried trim() (I use PHP 5+ so it should work). I also tried changing the file extension ".html" to ".php" just in case it would do something, but it doesn't.
Note2: Even though I know I could do it some other way, I want to get the country of the user through JavaScript and not through PHP. The reason is that I can't do it via server using my current hosting plan, so I thought of JavaScript instead.
Thank you very much.
I think you're muddled about when the various scripts are executed.
$content = file_get_contents(script.html);
At this point, $content contains the <script> tags. The JavaScript has not been executed as the JavaScript has not yet been sent to the browser.
echo "http://www.example.com/$content/example"
That sends this to the browser:
http://www.example.com/<script language="Javascript" src="http://www.codehelper.io/api/ips/?js"></script>
<script language="Javascript">document.write(codehelper_ip.Country);</script>/example
Since the PHP is so trivial, try this plain JavaScript solution instead:
<script language="Javascript" src="http://www.codehelper.io/api/ips/?js"></script>
<script language="Javascript">document.write("http://www.example.com/" + codehelper_ip.Country + "/example");</script>

What's the correct way to write this jquery attr code? (wordpress)

I have this in my theme's function file:
$(document).ready(function() {
$(".ajax-loader").attr("src","<?php bloginfo('template_url'); ?>/images/ajax-loader.gif");
});
However, when it prints, the src prints as <?php bloginfo('template_url'); ?>/images/ajax-loader.gif, that is showing the php code instead of showing my template url. What would be the correct way to write this code?
Is this in a .js file? If so, you can't place WordPress template tags inside because .js files are not processed by PHP.
You can either include your code inline within header.php using <script> tags:
<script type="text/javascript">
$(document).ready(function() {
$(".ajax-loader").attr("src","<?php bloginfo('template_url'); ?>/images/ajax-loader.gif");
});
</script>
Or change the extension of your JavaScript file from .js to .php, and add this line at the very top:
<?php header('Content-Type: text/javascript'); ?>
That tells the server to treat this as a JavaScript file, although it'll be processed by PHP.
In any case, you're using .attr() correctly.

Categories