inherit
223366
0
Jun 20, 2024 5:38:45 GMT -8
mikemcd
176
July 2015
mikemcd
|
Post by mikemcd on Jan 19, 2016 18:55:41 GMT -8
Hello, I will probably be posting questions often as I am new to creating plugins and using javascript. I do quite like it, though! I'm not sure if it's better just to create my own thread to house my novice questions or if it's better to make individual threads that may be useful for others later like me that might be looking for specific answers. I will choose the latter, but I'm open to suggestions.
Right then. To the point.
I don't want my plugin to load on every page in the forum. What is the proper way to restrict it to a certain custom page?
Also, what is the best form field when setting required user settings for choosing the custom page? I'm thinking a field that only accepts a URL.
Thanks to anyone who helps me get my plugin legs under me!
|
|
inherit
224902
0
Feb 15, 2017 12:50:39 GMT -8
Matt
2,940
September 2015
mattyboo1
|
Post by Matt on Jan 19, 2016 19:38:49 GMT -8
Unfortunately you cannot restrict it to a custom page, the code executes on the entire forum regardless of whether that page actually has any content from the plugin on it ( as far as I know )
|
|
inherit
223366
0
Jun 20, 2024 5:38:45 GMT -8
mikemcd
176
July 2015
mikemcd
|
Post by mikemcd on Jan 19, 2016 19:44:17 GMT -8
If that is the case, can I have javascript that is run on a custom page without using a plugin?
|
|
inherit
217348
0
Jul 27, 2022 7:26:44 GMT -8
Lynx
5,824
January 2015
msg
|
Post by Lynx on Jan 19, 2016 20:12:56 GMT -8
Unfortunately you cannot restrict it to a custom page, the code executes on the entire forum regardless of whether that page actually has any content from the plugin on it ( as far as I know ) No offense Matt, but you need to stop posting things that you don't know about. You've already admitted that you know nothing about coding. Please do not respond to a post if you aren't sure of your information. Bad information can cause more headaches than necessary. mikemcd - I don't know the exact syntax, but I do believe there is a pb.data('route').name that should be able to detect custom pages. None of my forums have a custom page, but you should be able to go to your custom page and look at the URL. If it's something like: http://yoursite.proboards.com/page/custom_page_name you should be able to test for it via pb.data('route').name == "page" - again, I don't have a test page to test this, so my code may not be the correct syntax. I have used pb.data('route').name == "home" to test for the home page in one of my plugins. My apologies for not being extremely helpful (still learning JS myself), but hopefully gives you a good starting point for just wanting to have it run on a custom page. Also, what is the best form field when setting required user settings for choosing the custom page? I'm thinking a field that only accepts a URL. Sorry - the underlined part confused me. If it helps, if your looking to have a field that goes to a URL, then you would want the field to only accept a URL. Not sure what settings you're referring to though.
|
|
inherit
223366
0
Jun 20, 2024 5:38:45 GMT -8
mikemcd
176
July 2015
mikemcd
|
Post by mikemcd on Jan 19, 2016 20:46:47 GMT -8
Lynx Thank you. I think that method will still place some load time on the main forum pages. This should work fine, though. About the underlined bit: I see that I can require some parameters to be filled out when the plugin is installed before it can be enabled. I think the URL or custom page ID needs to be filled in by the user for the sake of the plugin knowing where to run. I can set different fields up in a form to gather that info from the user, but I'm not sure which option would be best. I can select the field that forces them to add a URL, and if they paste their custom URL there it may work out.
|
|
inherit
216224
0
Aug 1, 2024 1:18:46 GMT -8
Quozzo
408
November 2014
quozzo
|
Post by Quozzo on Jan 20, 2016 0:15:05 GMT -8
A quick method to determine if you're viewing a page that you want to run JavaScript on is to simply check the URL. If you know regular expressions then you can check for several pages with a line of code, if not then stringing boolean checks would work too. (RegEx is not that hard to learn (just hard to master))
if(/page\/(custom|another)/.test(location.href)){ //do stuff here } The above will only run the code inside the if statement when a page that is called "custom" or "another" is being viewed. If you want it to run on any custom page then deleting the extra check so it's only checking for the "page" part will cause it to run on anything with "page/" in the URL. Note that the forward slash to check in the URL is escaped with a backslash as it's part of RegEx.
Another method is to use the String objects indexOf method. It works by returning the index of the first letter in the matched word, which means that if the matched word is at the beginning then it will return 0 which can cause problems to noobs, as that will be evaluated to false in an if statement. If there is no match then it will return -1, so you need to check it if is not -1 to be on a page you want to run some JS on.
if(location.href.indexOf("/page/") != -1){ //do stuff here }
if(location.href.indexOf("page/custom") != -1 || location.href.indexOf("page/another") != -1){ //do stuff here }
See how much longer that is compared to the equivalent RegEx above?
I usually just increase the value by 1 so that -1 = 0 and everything else is >0 or true
if(location.href.indexOf("/page/")+1){
//do stuff here
}
|
|
#e61919
Support Staff
224482
0
1
Oct 31, 2024 16:37:18 GMT -8
Scott
“Asking for help isn't giving up... it's refusing to give up.”
24,346
August 2015
socalso
|
Post by Scott on Jan 20, 2016 8:58:20 GMT -8
mikemcd & Lynx, Just to clarify, Matt was correct in his posting. The plugin code will load with the entire forum. And while you can write the code to execute a certain function on a particular page(s), the plugin code itself continues to run in the background and it will just check against the parameters that you've outlined as to whether or not it needs to perform it's function on said page.
|
|
inherit
201984
0
Sept 11, 2023 1:23:07 GMT -8
P̌̓aͧś̀t̀u͒le͆o͂2̀3̃̓
Using My Talents Elsewhere
3,314
November 2013
pastuleo23
|
Post by P̌̓aͧś̀t̀u͒le͆o͂2̀3̃̓ on Jan 20, 2016 12:42:12 GMT -8
Lynx Thank you. I think that method will still place some load time on the main forum pages. This should work fine, though. About the underlined bit: I see that I can require some parameters to be filled out when the plugin is installed before it can be enabled. I think the URL or custom page ID needs to be filled in by the user for the sake of the plugin knowing where to run. I can set different fields up in a form to gather that info from the user, but I'm not sure which option would be best. I can select the field that forces them to add a URL, and if they paste their custom URL there it may work out. Quozzos method is the best method to make the strain of loading the lowest. U can run that on document ready or slightly quicker in the footer tab of a global header footer component in your plugin
|
|
inherit
2671
0
May 14, 2013 14:40:03 GMT -8
Peter
🐺
10,615
February 2002
peter3
|
Post by Peter on Jan 20, 2016 13:56:51 GMT -8
If we are going for efficiency, then doing an equality test on the route (see Lynx's post) is the most efficient.
|
|
inherit
217348
0
Jul 27, 2022 7:26:44 GMT -8
Lynx
5,824
January 2015
msg
|
Post by Lynx on Jan 20, 2016 21:57:49 GMT -8
mikemcd & Lynx, Just to clarify, Matt was correct in his posting. The plugin code will load with the entire forum. And while you can write the code to execute a certain function on a particular page(s), the plugin code itself continues to run in the background and it will just check against the parameters that you've outlined as to whether or not it needs to perform it's function on said page. Okay, Scott. I guess it's me that's gotta keep my mouth shut then. I'll refrain from further commenting.
|
|
inherit
2671
0
May 14, 2013 14:40:03 GMT -8
Peter
🐺
10,615
February 2002
peter3
|
Post by Peter on Jan 21, 2016 1:47:15 GMT -8
If that is the case, can I have javascript that is run on a custom page without using a plugin? Seems this question was overlooked. The answer is Yes, you can. You can edit the HTML of a custom page, so you can add JS so it only runs on the custom page.
|
|
inherit
223366
0
Jun 20, 2024 5:38:45 GMT -8
mikemcd
176
July 2015
mikemcd
|
Post by mikemcd on Jan 21, 2016 5:45:09 GMT -8
If that is the case, can I have javascript that is run on a custom page without using a plugin? Seems this question was overlooked. The answer is Yes, you can. You can edit the HTML of a custom page, so you can add JS so it only runs on the custom page. Ah, see I'm so new to this kind of stuff that people would assume I would already know this. Thanks. I can run what I need to directly on the custom pages HTML. I wonder how that will tie to the key I will save data in though. At the very least I can put the bulk of the code in that page and the plugin can be slim. The only tricky part is that if anyone else wants to install the plugin I will have to include instructions for pasting a bit of JS into the HTML of their custom page. Everyone's comments are very helpful. This is such a good community!
|
|
inherit
2671
0
May 14, 2013 14:40:03 GMT -8
Peter
🐺
10,615
February 2002
peter3
|
Post by Peter on Jan 21, 2016 5:58:11 GMT -8
Seems this question was overlooked. The answer is Yes, you can. You can edit the HTML of a custom page, so you can add JS so it only runs on the custom page. Ah, see I'm so new to this kind of stuff that people would assume I would already know this. Thanks. I can run what I need to directly on the custom pages HTML. I wonder how that will tie to the key I will save data in though. At the very least I can put the bulk of the code in that page and the plugin can be slim. The only tricky part is that if anyone else wants to install the plugin I will have to include instructions for pasting a bit of JS into the HTML of their custom page. Everyone's comments are very helpful. This is such a good community! I would personally do a route check in the plugin. If you plan to distribute the plugin, then it will be a lot easier then having to provide steps for setting up a custom page and having them paste the code in. You can create blank pages without the need to create a custom page via the admin area. Monetary Shop, System, and Trophies does that. Yootil has some methods to help create pages, adding to the nav branch etc. yootil.pixeldepth.net/#!/api/yootil.create-method-page Edit: Seems ProBoards doesn't parse the link correctly. Copy and paste into URL instead Edit again: Don't feel like you need to use Yootil, it's open source, so take out the bit you need.
|
|
#e61919
Support Staff
224482
0
1
Oct 31, 2024 16:37:18 GMT -8
Scott
“Asking for help isn't giving up... it's refusing to give up.”
24,346
August 2015
socalso
|
Post by Scott on Jan 21, 2016 8:10:52 GMT -8
mikemcd & Lynx , Just to clarify, Matt was correct in his posting. The plugin code will load with the entire forum. And while you can write the code to execute a certain function on a particular page(s), the plugin code itself continues to run in the background and it will just check against the parameters that you've outlined as to whether or not it needs to perform it's function on said page. Okay, Scott . I guess it's me that's gotta keep my mouth shut then. I'll refrain from further commenting. Hey man, I hope you didn't think my post was negative against you or anything. It's just I've heard this question before about how plugins run and had to ask about it myself to better understand, so just thought I'd share and clear it up. All is good!
|
|